I want to add third-party analytics to my game but don’t know how since this guide seems to have fallen apart. Could somebody provide some tips on how to go about this?
That old guide used a ModuleScript from 2014 that used a HttpService postAsync to send data to Google’s servers.
Could this be adapted to use Google Analytics 4 instead of Universal Analytics? The GA4 documentation I looked at seemed to insist on inserting JavaScript into a web page.
local HTTPService = game:GetService("HttpService")
local MeasurementId = "G-XXXXXXXXXX"
local ApiSecret = "xXXxXXXXXXxXXxXXXXXXXx"
local ServerSessionId = game:GetService("HttpService"):GenerateGUID()
local url = string.format("https://www.google-analytics.com/mp/collect?measurement_id=%s&api_secret=%s", MeasurementId, ApiSecret)
local data = HTTPService:JSONEncode({
client_id = game.JobId;
events = {
name = 'UserVisit',
params = {
exampleParam = 1234;
},
}
})
while wait(5) do
print("response: ", HTTPService:PostAsync(url, data, Enum.HttpContentType.ApplicationJson))
end
Placing this code in a server script will result in an event being sent every 5 seconds.
I don’t see any change in my analytics dashboard on the main page at first
“In order for user activity to display in reports like Realtime, engagement_time_msec and session_id must be supplied as part of the params for an event .”
So I updated my code to the following:
local HTTPService = game:GetService("HttpService")
local MeasurementId = "G-XXXXXXXXXX"
local ApiSecret = "xXXxXXXXXXxXXxXXXXXXXx"
local ServerSessionId = game:GetService("HttpService"):GenerateGUID()
local url = string.format("https://www.google-analytics.com/mp/collect?measurement_id=%s&api_secret=%s", MeasurementId, ApiSecret)
local data = HTTPService:JSONEncode({
client_id = game.JobId;
events = {
name = 'UserVisit',
params = {
exampleParam = 1234;
engagement_time_msec = "100";
session_id = "123";
},
}
})
while wait(5) do
print("response: ", HTTPService:PostAsync(url, data, Enum.HttpContentType.ApplicationJson))
end
Now I need to figure out what other secret parameters I can use to make the dashboard acknowledge the data I’m sending it.