mirror of
https://github.com/thegatesbrowser/thegates.git
synced 2025-08-23 08:17:34 -04:00
analytics handles
This commit is contained in:
parent
689afadaea
commit
b21d008ec1
5 changed files with 62 additions and 16 deletions
|
@ -25,6 +25,7 @@ config/windows_native_icon="res://the_gates/icons/icon.ico"
|
|||
DataSaver="*res://the_gates/scripts/data_saver.gd"
|
||||
FileDownloader="*res://the_gates/scripts/loading/file_downloader.gd"
|
||||
Debug="*res://the_gates/scripts/debug-log/debug.gd"
|
||||
Analytics="*res://the_gates/scripts/analytics/analytics.gd"
|
||||
|
||||
[display]
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
[gd_resource type="Resource" script_class="BackendSettings" load_steps=2 format=3 uid="uid://cjcdum6fm4ta0"]
|
||||
|
||||
[ext_resource type="Script" path="res://the_gates/scripts/analytics/backend_settings.gd" id="1_8fkk2"]
|
||||
[ext_resource type="Script" path="res://the_gates/scripts/analytics/backend.gd" id="1_6ck4k"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_8fkk2")
|
||||
url = "http://95.163.241.188:8000/"
|
||||
script = ExtResource("1_6ck4k")
|
||||
local_url = "http://127.0.0.1:8000"
|
||||
remote_url = "http://95.163.241.188:8000"
|
||||
host_type = 0
|
|
@ -1,24 +1,51 @@
|
|||
extends Node
|
||||
#class_name Analitycs
|
||||
|
||||
var backend_settings = preload("res://the_gates/resources/backend_settings.tres")
|
||||
var handle = "api/analytics"
|
||||
var backend := preload("res://the_gates/resources/backend.tres")
|
||||
var user_id := "none"
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
send_event("application_enter")
|
||||
await get_user_id()
|
||||
send_event({
|
||||
"event_name" : "application_enter",
|
||||
"user_id" : user_id
|
||||
})
|
||||
|
||||
|
||||
func send_event(name: String, body: Variant = null) -> void:
|
||||
var url = backend_settings.url + handle
|
||||
func send_event(body: Variant = []) -> void:
|
||||
var url = backend.analytics_event
|
||||
var callback = func(result, code, headers, body):
|
||||
if code != 200: Debug.logerr("Request send_event failed. Code " + str(code))
|
||||
|
||||
var err = await request(url, callback, body, HTTPClient.METHOD_POST)
|
||||
if err != HTTPRequest.RESULT_SUCCESS: Debug.logerr("Cannot send request send_event")
|
||||
|
||||
|
||||
func get_user_id() -> void:
|
||||
var url = backend.get_user_id + OS.get_unique_id()
|
||||
var callback = func(result, code, headers, body):
|
||||
if code == 200:
|
||||
user_id = body.get_string_from_utf8()
|
||||
Debug.logr("User id recieved: " + user_id)
|
||||
else: Debug.logerr("Request get_user_id failed. Code " + str(code))
|
||||
|
||||
var err = await request(url, callback)
|
||||
if err != HTTPRequest.RESULT_SUCCESS: Debug.logerr("Cannot send request get_user_id")
|
||||
|
||||
|
||||
func request(url: String, callback: Callable,
|
||||
body: Variant = [], method: int = HTTPClient.METHOD_GET) -> Error:
|
||||
var data = JSON.stringify(body)
|
||||
var headers = []
|
||||
|
||||
var http = HTTPRequest.new()
|
||||
http.use_threads = true
|
||||
add_child(http)
|
||||
|
||||
var err = http.request(url, [], HTTPClient.METHOD_POST, data)
|
||||
await http.request_completed
|
||||
var err = http.request(url, headers, method, data)
|
||||
var res = await http.request_completed
|
||||
callback.call(res[0], res[1], res[2], res[3])
|
||||
remove_child(http)
|
||||
|
||||
if err != HTTPRequest.RESULT_SUCCESS:
|
||||
Debug.logerr("Analitycs event is not sent. Name: " + name)
|
||||
return err
|
||||
|
|
20
project/the_gates/scripts/analytics/backend.gd
Normal file
20
project/the_gates/scripts/analytics/backend.gd
Normal file
|
@ -0,0 +1,20 @@
|
|||
extends Resource
|
||||
class_name BackendSettings
|
||||
|
||||
enum HostType {
|
||||
Local,
|
||||
Remote
|
||||
}
|
||||
|
||||
@export var local_url: String
|
||||
@export var remote_url: String
|
||||
@export var host_type: HostType
|
||||
|
||||
var url: String :
|
||||
get: return local_url if host_type == HostType.Local else remote_url
|
||||
|
||||
var analytics_event: String :
|
||||
get: return url + "/api/analytics_event"
|
||||
|
||||
var get_user_id: String :
|
||||
get: return url + "/api/get_user_id?device_id="
|
|
@ -1,4 +0,0 @@
|
|||
extends Resource
|
||||
class_name BackendSettings
|
||||
|
||||
@export var url: String
|
Loading…
Add table
Add a link
Reference in a new issue