send exit events on startup

This commit is contained in:
Nordup 2023-06-05 00:26:52 +03:00
parent 7f8d67f6f4
commit a5fe259dce
5 changed files with 26 additions and 13 deletions

View file

@ -14,11 +14,6 @@ func _ready() -> void:
add_child(sender) add_child(sender)
func _exit_tree() -> void:
for sender in analytics_senders:
sender.exit()
func send_event(body: Dictionary = {}) -> void: func send_event(body: Dictionary = {}) -> void:
var url = backend.analytics_event var url = backend.analytics_event
var callback = func(_result, code, _headers, _body): var callback = func(_result, code, _headers, _body):
@ -33,7 +28,6 @@ func get_user_id() -> void:
var callback = func(_result, code, _headers, body): var callback = func(_result, code, _headers, body):
if code == 200: if code == 200:
AnalyticsEvents.user_id = body.get_string_from_utf8() AnalyticsEvents.user_id = body.get_string_from_utf8()
Debug.logr("User id recieved: " + AnalyticsEvents.user_id)
else: Debug.logerr("Request get_user_id failed. Code " + str(code)) else: Debug.logerr("Request get_user_id failed. Code " + str(code))
var err = await request(url, callback) var err = await request(url, callback)

View file

@ -8,6 +8,12 @@ var heartbeat_timer: Timer
func _ready() -> void: func _ready() -> void:
Analytics.send_event(AnalyticsEvents.app_open()) Analytics.send_event(AnalyticsEvents.app_open())
start_heartbeat() start_heartbeat()
# Send latest exit event
var json: String = DataSaver.get_string("analytics", "app_exit")
if json.is_empty(): return
DataSaver.set_value("analytics", "app_exit", "")
Analytics.send_event(JSON.parse_string(json))
func start_heartbeat() -> void: func start_heartbeat() -> void:
@ -22,6 +28,8 @@ func send_hearbeat() -> void:
Analytics.send_event(AnalyticsEvents.heartbeat(time_spend)) Analytics.send_event(AnalyticsEvents.heartbeat(time_spend))
func exit() -> void: func _exit_tree() -> void:
# Save to send on open
var time_spend = int(Time.get_ticks_msec() / 1000) var time_spend = int(Time.get_ticks_msec() / 1000)
Analytics.send_event(AnalyticsEvents.app_exit(time_spend)) var event = AnalyticsEvents.app_exit(time_spend)
DataSaver.set_value("analytics", "app_exit", JSON.stringify(event))

View file

@ -11,6 +11,12 @@ func _ready() -> void:
gate_events.open_gate.connect(send_gate_open) gate_events.open_gate.connect(send_gate_open)
gate_events.gate_entered.connect(send_gate_enter) gate_events.gate_entered.connect(send_gate_enter)
gate_events.exit_gate.connect(send_gate_exit) gate_events.exit_gate.connect(send_gate_exit)
# Send latest exit event
var json: String = DataSaver.get_string("analytics", "send_gate_exit")
if json.is_empty(): return
DataSaver.set_value("analytics", "send_gate_exit", "")
Analytics.send_event(JSON.parse_string(json))
func send_gate_open(url: String) -> void: func send_gate_open(url: String) -> void:
@ -31,6 +37,10 @@ func send_gate_exit() -> void:
gate_url = "" gate_url = ""
func exit() -> void: func _exit_tree() -> void:
if gate_url.is_empty(): return if gate_url.is_empty(): return
send_gate_exit()
# Save to send on open
var time_spend = int(Time.get_ticks_msec() / 1000) - gate_open_time
var event = AnalyticsEvents.gate_exit(gate_url, time_spend)
DataSaver.set_value("analytics", "send_gate_exit", JSON.stringify(event))

View file

@ -1,4 +1,5 @@
extends ConfigBase extends ConfigBase
#class_name DataSaver
var path: String = "user://resources/data_saver.cfg" var path: String = "user://resources/data_saver.cfg"

View file

@ -15,7 +15,7 @@ func get_string(section: String, key: String) -> String:
var value: String var value: String
if config.has_section_key(section, key): if config.has_section_key(section, key):
value = config.get_value(section, key) value = config.get_value(section, key)
Debug.logr(key + "=" + value) # Debug.logr(key + "=" + value)
else: Debug.logclr("don't have section " + section + ", key " + key, Color.YELLOW) else: Debug.logclr("don't have section " + section + ", key " + key, Color.YELLOW)
return value return value
@ -24,7 +24,7 @@ func get_value(section: String, key: String):
var value var value
if config.has_section_key(section, key): if config.has_section_key(section, key):
value = config.get_value(section, key) value = config.get_value(section, key)
Debug.logr(key + "=" + str(value)) # Debug.logr(key + "=" + str(value))
else: Debug.logclr("don't have section " + section + ", key " + key, Color.YELLOW) else: Debug.logclr("don't have section " + section + ", key " + key, Color.YELLOW)
return value return value
@ -37,7 +37,7 @@ func get_section_keys(section: String) -> PackedStringArray:
var keys: PackedStringArray var keys: PackedStringArray
if config.has_section(section): if config.has_section(section):
keys = config.get_section_keys(section) keys = config.get_section_keys(section)
Debug.logr(keys) # Debug.logr(keys)
else: Debug.logclr("don't have section " + section, Color.YELLOW) else: Debug.logclr("don't have section " + section, Color.YELLOW)
return keys return keys