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)
func _exit_tree() -> void:
for sender in analytics_senders:
sender.exit()
func send_event(body: Dictionary = {}) -> void:
var url = backend.analytics_event
var callback = func(_result, code, _headers, _body):
@ -33,7 +28,6 @@ func get_user_id() -> void:
var callback = func(_result, code, _headers, body):
if code == 200:
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))
var err = await request(url, callback)

View file

@ -9,6 +9,12 @@ func _ready() -> void:
Analytics.send_event(AnalyticsEvents.app_open())
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:
heartbeat_timer = Timer.new()
@ -22,6 +28,8 @@ func send_hearbeat() -> void:
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)
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

@ -12,6 +12,12 @@ func _ready() -> void:
gate_events.gate_entered.connect(send_gate_enter)
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:
gate_url = url
@ -31,6 +37,10 @@ func send_gate_exit() -> void:
gate_url = ""
func exit() -> void:
func _exit_tree() -> void:
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
#class_name DataSaver
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
if config.has_section_key(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)
return value
@ -24,7 +24,7 @@ func get_value(section: String, key: String):
var value
if config.has_section_key(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)
return value
@ -37,7 +37,7 @@ func get_section_keys(section: String) -> PackedStringArray:
var keys: PackedStringArray
if config.has_section(section):
keys = config.get_section_keys(section)
Debug.logr(keys)
# Debug.logr(keys)
else: Debug.logclr("don't have section " + section, Color.YELLOW)
return keys