How to set up third-party analytics?

Roblox has a guide for adding third-party analytics to your game, but this guide seems to be out of date, using deprecated Google services, and is no longer followable.

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.

local hs = game:GetService("HttpService")
hs:PostAsync(
	"http://www.google-analytics.com/collect",
	"v=1&t=event&sc=start" ..
	"&tid=" .. id .. 
	"&cid=" .. googleUserTrackingId ..
	"&ec=" .. hs:UrlEncode(category) ..
	"&ea=" .. hs:UrlEncode(action) .. 
	"&el=" .. hs:UrlEncode(label) ..
	"&ev=" .. hs:UrlEncode(value),
	Enum.HttpContentType.ApplicationUrlEncoded
)

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.

I’m unsure of the new API as I’ve never used it, but I do know that it should be possible with HttpService.

It likely accepts JSON.

Here’s what I’ve got so far:

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


However, if I select “View realtime”, I get this:

So my problem now is; how come the data in that itty bitty box in the bottom right of this random page won’t show up anywhere else in the dashboard? As shown above, it still says I have zero users. Their documentation says I can’t use any event names reserved for their web services like “session_start” which makes things hard.

Ok I just made some more progress.

According to this page:

“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.

2 Likes

have you gotten anyfurther in this?

No. I gave up on Google Analytics and am using Game Analytics instead which has an official Roblox implementation. Their library has a few bugs I’ve needed to fix but otherwise works well and is decently powerful.

2 Likes

I wanted to use game analytics but I did not like their SDK module, it seems very overcomplicated with datastores etc.

I haven’t had any issues with its datastore usage cutting into my quota so far and my game uses datastores fairly heavily.

I didn’t like that it wanted me to put scripts in 3 different locations in every place I use it in so I modified it slightly so they would all work from a single folder in the workspace (I have a module that allows localscripts to run in the workspace)