forked from Montecri/Godot-GameAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameAnalytics.gd
699 lines (581 loc) · 21.6 KB
/
GameAnalytics.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
extends Node
# GameAnalytics <https://gameanalytics.com/> native GDScript REST API implementation
# Cross-platform. Should work in every platform supported by Godot
# Adapted from REST_v2_example.py by Cristiano Reis Monteiro <[email protected]> Abr/2018
""" Procedure -->
1. make an init call
- check if game is disabled
- calculate client timestamp offset from server time
2. start a session
3. add a user event (session start) to queue
4. add a business event + some design events to queue
5. submit events in queue
6. add some design events to queue
7. add session_end event to queue
8. submit events in queue
"""
# From https://github.com/xsellier/godot-uuid
const UUID = preload("res://uuid.gd")
# device information
var DEBUG = false
var returned
var response_data
var response_code
#var uuid = "000002"
var uuid = UUID.v4()
# For some reason GameAnalytics only accepts lower case. Weird but happened to me
var platform = OS.get_name().to_lower()
#var os_version = OS.get_name()
# Couldn't find a way to get OS version yet. Need to adapt for iOS or anything else
var os_version = "android 4.4.4"
var sdk_version = 'rest api v2'
var device = OS.get_model_name().to_lower()
var manufacturer = OS.get_name().to_lower()
# game information
var build_version = 'alpha 0.0.1'
var engine_version = 'Godot 3.0.2'
# sandbox game keys
var game_key = "5c6bcb5402204249437fb5a7a80a4959"
var secret_key = "16813a12f718bc5c620f56944e1abc3ea13ccbac"
# sandbox API urls
var base_url = "http://sandbox-api.gameanalytics.com"
var url_init = "/v2/" + game_key + "/init"
var url_events = "/v2/" + game_key + "/events"
# settings
var use_gzip = false
var verbose_log = false
# global state to track changes when code is running
var state_config = {
# the amount of seconds the client time is offset by server_time
# will be set when init call receives server_time
'client_ts_offset': 0,
# will be updated when a new session is started
'session_id': uuid,
# set if SDK is disabled or not - default enabled
'enabled': true,
# event queue - contains a list of event dictionaries to be JSON encoded
'event_queue': []
}
var requests = HTTPClient.new()
func _ready():
# # For some reason, "Windows" is not recognized by GameAnaytics. Guess they only expect mobile platforms
# if platform == "Windows":
# platform = "ios"
#
# if os_version == "Windows":
# os_version = "ios 8.2"
#
# post_to_log("")
# post_to_log("-------- GameAnalytics REST V2 -------------")
# #-->post_to_log(("Gzip enabled!" if use_gzip else "Gzip disabled!")
# post_to_log("--------------------------------------------")
# 1. init call
#init_response, init_response_code = request_init()
# var init_response = request_init()
# var init_response_code = init_response
# calculate client_ts offset from server
#update_client_ts_offset(init_response['server_ts'])
# post_to_log("Init call successful !")
# print_verbose("Init response: " + str(init_response))
#
# # 2. send events (only one business event)
# if !state_config['enabled']:
# post_to_log("SDK disabled. Will not continue event submit.")
# #sys.exit()
# generate session id
#generate_new_session_id()
#annotate_event_with_default_values()
# add_to_event_queue(get_test_design_event("player:new_level", 1))
# add_to_event_queue(get_test_design_event("player:new_level", 2))
# add_to_event_queue(get_test_design_event("player:played_video", 3))
#
# returned = submit_events()
# # add session start event (user event)
# add_to_event_queue(get_test_user_event())
#
# # add business event
# add_to_event_queue(get_test_business_event_dict())
#
# # add design events
# add_to_event_queue(get_test_design_event("player:death:shotgun", 0))
# add_to_event_queue(get_test_design_event("player:kill:blue_yeti", 23.9))
# add_to_event_queue(get_test_design_event("player:kill:black_yeti", 90.3))
#
# # submit the event queue and clear it
# returned = submit_events()
## response_data = returned.x
## response_code = returned.y
#
# # add more design events
# add_to_event_queue(get_test_design_event("player:kill:black_yeti", 90.9))
# add_to_event_queue(get_test_design_event("player:death:black_yeti", 0))
# add_to_event_queue(get_test_design_event("player:kill:golden_goose", 21.5))
#
# # end session: add session end event
# add_to_event_queue(get_test_session_end_event(200))
#
# # submit the event queue and clear it
# returned = submit_events()
# response_data = returned.x
# response_code = returned.y
#sys.exit()
pass
#func _process(delta):
# # Called every frame. Delta is time since last frame.
# # Update game logic here.
# pass
#import requests
## install the requests Python package
## pip install requests or easy_install requests
#import json
#import hmac
#import base64
#import hashlib
#import sys
#from datetime import datetime
#import calendar
#import uuid
#import gzip
#from StringIO import StringIO
# main entry point
#func run():
# adding an event to the queue for later submit
func add_to_event_queue(event_dict):
# if not isinstance(event_dict, dict):
# return
#annotate_event_with_default_values(event_dict)
#annotate_event_with_default_values()
# Load saved events, if any
var f = File.new()
if f.file_exists("user://event_queue"):
f.open("user://event_queue", File.READ)
state_config['event_queue'] = f.get_var()
f.close()
state_config['event_queue'].append(event_dict)
#Save to file
f.open("user://event_queue", File.WRITE)
f.store_var(state_config['event_queue'])
f.close()
# requesting init URL and returning result
func request_init():
# Get version number on Android. Need something similar for iOS
if platform == "android":
var output = []
var pid = OS.execute("getprop", ["ro.build.version.release"], true, output)
# Trimming new line char at the end
output[0] = output[0].substr(0, output[0].length() - 1)
os_version = platform + " " + output[0]
var init_payload = {
'platform': platform,
'os_version': os_version,
'sdk_version': sdk_version
}
# generate session id
generate_new_session_id()
# Refreshing url_init since game key might have been changed externally
url_init = "/v2/" + game_key + "/init"
#var queryString = requests.query_string_from_dict(init_payload)
#var init_payload_json = json.dumps(init_payload)
var init_payload_json = to_json(init_payload)
var headers = [
"Authorization: " + Marshalls.raw_to_base64(hmac_sha256(init_payload_json, secret_key)),
"Content-Type: application/json"]
#var headers = ["\"Authorization: " + Marshalls.raw_to_base64(hmac_sha256(init_payload_json, secret_key)) + "\"", "\"Content-Type: application/json\""]
print(Marshalls.raw_to_base64(hmac_sha256(init_payload_json, secret_key)))
#response_dict = None
#status_code = None
var response_dict
var status_code
if DEBUG:
print(base_url)
print(url_init)
print(init_payload_json)
print(Marshalls.raw_to_base64(hmac_sha256(init_payload_json, secret_key)))
#try:
var err = requests.connect_to_host(base_url,80)
# Wait until resolved and connected
while requests.get_status() == HTTPClient.STATUS_CONNECTING or requests.get_status() == HTTPClient.STATUS_RESOLVING:
requests.poll()
print("Connecting..")
OS.delay_msec(500)
# Error catch: Could not connect
#assert(requests.get_status() == HTTPClient.STATUS_CONNECTED)
#var h = [to_json(headers)]
var init_response = requests.request(HTTPClient.METHOD_POST, url_init, headers, init_payload_json)
# except:
# post_to_log("Init request failed!")
# sys.exit()
while requests.get_status() == HTTPClient.STATUS_REQUESTING:
# Keep polling until the request is going on
requests.poll()
print("Requesting..")
OS.delay_msec(500)
if requests.has_response():
# If there is a response..
headers = requests.get_response_headers_as_dictionary() # Get response headers
print("code: ", requests.get_response_code()) # Show response code
print("**headers:\\n", headers) # Show headers
# Getting the HTTP Body
if requests.is_response_chunked():
# Does it use chunks?
print("Response is Chunked!")
else:
# Or just plain Content-Length
var bl = requests.get_response_body_length()
print("Response Length: ",bl)
# This method works for both anyway
var rb = PoolByteArray() # Array that will hold the data
while requests.get_status() == HTTPClient.STATUS_BODY:
# While there is body left to be read
requests.poll()
var chunk = requests.read_response_body_chunk() # Get a chunk
if chunk.size() == 0:
# Got nothing, wait for buffers to fill a bit
OS.delay_usec(1000)
else:
rb = rb + chunk # Append to read buffer
# Done!
print("bytes got: ", rb.size())
var text = rb.get_string_from_ascii()
print("Text: ", text)
#status_code = init_response.status_code
status_code = requests.get_response_code()
#try:
response_dict = to_json(init_response)
# except:
# response_dict = None
# if not isinstance(status_code, (long, int)):
# post_to_log("---- Submit Init ERROR ----")
# post_to_log("URL: " + str(url_init))
# post_to_log("Payload JSON: " + str(init_payload_json))
# post_to_log("Headers: " + str(headers))
# var a
# if status_code is null:
# a = ""
# else a = "Returned: " + str(status_code) + " response code."
var response_string = (status_code)
if status_code == 401:
post_to_log("Submit events failed due to UNAUTHORIZED.")
post_to_log("Please verify your Authorization code is working correctly and that your are using valid game keys.")
#sys.exit()
if status_code != 200:
post_to_log("Init request did not return 200!")
post_to_log(response_string)
# if isinstance(response_dict, dict):
# post_to_log(response_dict)
# elif isinstance(init_response.text, basestring):
# post_to_log("Response contents: " + init_response.text)
#sys.exit()
# init request ok --> get values
# if 'server_ts' not in response_dict or not isinstance(response_dict['server_ts'], (int, long)):
# post_to_log("Init request failed! Did not return proper server_ts..")
# sys.exit()
# if 'enabled' in response_dict and !response_dict['enabled']:
# state_config['enabled'] = false
#return Vector2(response_dict, status_code)
return status_code
# submitting all events that are in the queue to the events URL
func submit_events():
#try:
# Refreshing url_events since game key might have been changed externally
url_events = "/v2/" + game_key + "/events"
var event_list_json = to_json(state_config['event_queue'])
# except:
# post_to_log("Event queue failed JSON encoding!")
# event_list_json = None
# return
# if event_list_json is null:
# return
# if gzip enabled
if use_gzip:
event_list_json = get_gzip_string(event_list_json)
# create headers with authentication hash
var headers = [
"Authorization: " + Marshalls.raw_to_base64(hmac_sha256(event_list_json, secret_key)),
"Content-Type: application/json"]
# if gzip enabled add the encoding header
if use_gzip:
headers.append('Content-Encoding: gzip')
var err = requests.connect_to_host(base_url,80)
# Wait until resolved and connected
while requests.get_status() == HTTPClient.STATUS_CONNECTING or requests.get_status() == HTTPClient.STATUS_RESOLVING:
requests.poll()
print("Connecting..")
OS.delay_msec(500)
#try:
var events_response = requests.request(HTTPClient.METHOD_POST, url_events, headers, event_list_json)
# except Exception as e:
# post_to_log("Submit events request failed!")
# post_to_log(e.reason)
# return (None, None)
while requests.get_status() == HTTPClient.STATUS_REQUESTING:
# Keep polling until the request is going on
requests.poll()
print("Requesting..")
OS.delay_msec(500)
if requests.has_response():
# If there is a response..
headers = requests.get_response_headers_as_dictionary() # Get response headers
print("code: ", requests.get_response_code()) # Show response code
print("**headers:\\n", headers) # Show headers
# Getting the HTTP Body
if requests.is_response_chunked():
# Does it use chunks?
print("Response is Chunked!")
else:
# Or just plain Content-Length
var bl = requests.get_response_body_length()
print("Response Length: ",bl)
# This method works for both anyway
var rb = PoolByteArray() # Array that will hold the data
while requests.get_status() == HTTPClient.STATUS_BODY:
# While there is body left to be read
requests.poll()
var chunk = requests.read_response_body_chunk() # Get a chunk
if chunk.size() == 0:
# Got nothing, wait for buffers to fill a bit
OS.delay_usec(1000)
else:
rb = rb + chunk # Append to read buffer
# Done!
print("bytes got: ", rb.size())
var text = rb.get_string_from_ascii()
print("Text: ", text)
#var status_code = events_response.status_code
var status_code = requests.get_response_code()
#try:
#var response_dict = events_response.json()
# except:
# response_dict = None
#print_verbose("Submit events response: " + str(response_dict))
# check response code
#status_code_string = ("" if status_code is None else "Returned: " + str(status_code) + " response code.")
var status_code_string = str(status_code)
if status_code == 400:
post_to_log(status_code_string)
post_to_log("Submit events failed due to BAD_REQUEST.")
# If bad request, then some parameter is very wrong. Eliminating queue in order to no hold submission
# In future instances where the bad parameter is no longer existing
state_config['event_queue'] = []
var dir = Directory.new()
dir.remove("user://event_queue")
# if isinstance(response_dict, (dict, list)):
# post_to_log("Payload found in response. Contents can explain what fields are causing a problem.")
# post_to_log(events_response.text)
# sys.exit()
# elif status_code == 401:
# post_to_log(status_code_string)
# post_to_log("Submit events failed due to UNAUTHORIZED.")
# post_to_log("Please verify your Authorization code is working correctly and that your are using valid game keys.")
# sys.exit()
elif status_code != 200:
post_to_log(status_code_string)
post_to_log("Submit events request did not succeed! Perhaps offline.. ")
# sys.exit()
# if not isinstance(response_dict, dict):
# post_to_log("Submit events request returned 200, but reading and JSON decoding response failed..")
# post_to_log(events_response.text)
# sys.exit()
if status_code == 200:
post_to_log("Events submitted !")
# clear event queue
# If submitte successfully, then clear queue and remove queu file to not create duplicate entries
state_config['event_queue'] = []
var dir = Directory.new()
dir.remove("user://event_queue")
else:
post_to_log("Event submission FAILED!")
return status_code
# ------------------ HELPER METHODS ---------------------- #
func generate_new_session_id():
state_config['session_id'] = uuid
print_verbose("Session Id: " + state_config['session_id'])
func update_client_ts_offset(server_ts):
# calculate client_ts using offset from server time
#datetime.utcnow()
var now_ts = OS.get_unix_time_from_datetime(OS.get_datetime())
# var client_ts = calendar.timegm(now_ts.timetuple())
var client_ts = now_ts
var offset = client_ts - server_ts
# --> Verificar
#offset = 0
# if too small difference then ignore
if offset < 10:
state_config['client_ts_offset'] = 0
else:
state_config['client_ts_offset'] = offset
print_verbose('Client TS offset calculated to: ' + str(offset))
#func hmac_hash_with_secret(message, key):
# #requests.
# return utf8_to_base64(hmac.new(key, message, hashlib.sha256))
func get_test_business_event_dict():
var event_dict = {
'category': 'business',
'amount': 999,
'currency': 'USD',
'event_id': 'Weapon:SwordOfFire', # item_type:item_id
'cart_type': 'MainMenuShop',
'transaction_num': 1, # should be incremented and stored in local db
'receipt_info': {'receipt': 'xyz', 'store': 'apple'} # receipt is base64 encoded receipt
}
return event_dict
func get_test_user_event():
var event_dict = {
'category': 'user'
}
return event_dict
func get_test_session_end_event(length_in_seconds):
var event_dict = {
'category': 'session_end',
'length': length_in_seconds
}
return event_dict
func get_test_design_event(event_id, value):
var event_dict = {
'category': 'design',
'event_id': event_id,
'value': value
}
merge_dir(event_dict, annotate_event_with_default_values())
#annotate_event_with_default_values()
# if isinstance(value, (int, long, float)):
# event_dict['value'] = value
return event_dict
static func merge_dir(target, patch):
for key in patch:
target[key] = patch[key]
static func merge_dir2(target, patch):
for key in patch:
if target.has(key):
var tv = target[key]
if typeof(tv) == TYPE_DICTIONARY:
merge_dir(tv, patch[key])
else:
target[key] = patch[key]
else:
target[key] = patch[key]
func get_gzip_string(string_for_gzip):
var f = File.new()
f.open_compressed("user://gzip", File.WRITE, File.COMPRESSION_GZIP)
#f.store_buffer(string_for_gzip.to_utf8())
f.store_string(string_for_gzip)
f.close()
f.open("user://gzip", File.READ)
#var enc_text = f.get_buffer(f.get_len())
#get_string_from_utf8()
var enc_text = f.get_as_text()
#var enc_text = f.get_buffer(f.get_len()).get_string_from_utf8()
f.close()
# var zip_text_file = StringIO()
# var zipper = gzip.GzipFile('wb', zip_text_file)
# zipper.write(string_for_gzip)
# zipper.close()
#
# enc_text = zip_text_file.getvalue()
return enc_text
pass
# add default annotations (will alter the dict by reference)
#func annotate_event_with_default_values(event_dict):
func annotate_event_with_default_values():
var now_ts = OS.get_datetime()
# datetime.utcnow()
# calculate client_ts using offset from server time
#var client_ts = calendar.timegm(now_ts.timetuple()) - state_config['client_ts_offset']
var client_ts = OS.get_unix_time_from_datetime(OS.get_datetime())
# TEST IDFA / IDFV
#var idfa = 'AEBE52E7-03EE-455A-B3C4-E57283966239'
var idfa = OS.get_unique_id().to_lower()
var idfv = 'AEBE52E7-03EE-455A-B3C4-E57283966239'
var default_annotations = {
'v': 2, # (required: Yes)
'user_id': idfa, # (required: Yes)
#'ios_idfa': idfa, # (required: No - required on iOS)
#'ios_idfv': idfv, # (required: No - send if found)
# 'google_aid' # (required: No - required on Android)
# 'android_id', # (required: No - send if set)
# 'googleplus_id', # (required: No - send if set)
# 'facebook_id', # (required: No - send if set)
# 'limit_ad_tracking', # (required: No - send if true)
# 'logon_gamecenter', # (required: No - send if true)
# 'logon_googleplay # (required: No - send if true)
#'gender': 'male', # (required: No - send if set)
# 'birth_year # (required: No - send if set)
# 'progression # (required: No - send if a progression attempt is in progress)
#'custom_01': 'ninja', # (required: No - send if set)
# 'custom_02 # (required: No - send if set)
# 'custom_03 # (required: No - send if set)
'client_ts': client_ts, # (required: Yes)
'sdk_version': sdk_version, # (required: Yes)
'os_version': os_version, # (required: Yes)
'manufacturer': manufacturer, # (required: Yes)
'device': device, # (required: Yes - if not possible set "unknown")
'platform': platform, # (required: Yes)
'session_id': state_config['session_id'], # (required: Yes)
#'build': build_version, # (required: No - send if set)
'session_num': 1, # (required: Yes)
#'connection_type': 'wifi', # (required: No - send if available)
# 'jailbroken # (required: No - send if true)
#'engine_version': engine_version # (required: No - send if set by an engine)
}
#event_dict.update(default_annotations)
#state_config['event_queue'].append(default_annotations)
return default_annotations
func print_verbose(message):
print(message)
if verbose_log:
post_to_log(message)
func post_to_log(message):
print(message)
pass
# -- RUN -- #
#run()
func hmac_sha256(message, key):
var x = 0
var k
if key.length() <= 64:
k = key.to_utf8()
# Hash key if length > 64
if key.length() > 64:
k = key.sha256_buffer()
# Right zero padding if key length < 64
while k.size() < 64:
k.append(convert_hex_to_dec("00"))
var i = "".to_utf8()
var o = "".to_utf8()
var m = message.to_utf8()
var s = File.new()
while x < 64:
o.append(k[x] ^ 0x5c)
i.append(k[x] ^ 0x36)
x += 1
var inner = i + m
s.open("user://temp", File.WRITE)
s.store_buffer(inner)
s.close()
var z = s.get_sha256("user://temp")
var outer = "".to_utf8()
x = 0
while x < 64:
outer.append(convert_hex_to_dec(z.substr(x, 2)))
x += 2
outer = o + outer
s.open("user://temp", File.WRITE)
s.store_buffer(outer)
s.close()
z = s.get_sha256("user://temp")
outer = "".to_utf8()
x = 0
while x < 64:
outer.append(convert_hex_to_dec(z.substr(x, 2)))
x += 2
var mm = outer
return outer
func convert_hex_to_dec(h):
var c = "0123456789ABCDEF"
h = h.to_upper()
var r = h.right(1)
var l = h.left(1)
var b0 = c.find(r)
var b1 = c.find(l) * 16
var x = b1 + b0
return x