## Google Analytics library (using Measurement protocol)

dusoft
Party member
Posts: 111
Joined: Fri Nov 08, 2013 12:07 am

### Google Analytics library (using Measurement protocol)

You like stats? It could be fun to know which parts of your game players get to or what's their inventory or if they ever press that special button you made.

Now you can - just use this Google Analytics class for LÖVE:
https://github.com/nekromoff/analyticslove

Currently just three basic types of reporting are supported:
- pageview
- event
- ecommerce / transaction + product

Feedback is welcome.

PS: If you use this class, please inform users beforehand and allow them to disable it, if they want.

zorg
Party member
Posts: 3041
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Google Analytics library (using Measurement protocol)

One thing, this page says that the uuid function should be type 4 according to the linked rfi:
"The value of this field should be a random UUID (version 4) as described in http://www.ietf.org/rfc/rfc4122.txt."

https://en.wikipedia.org/wiki/Universal ... 4_(random)
According to the above link, the implementation of the UUID should have this signature to be correct:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
where M is the hex digit 4 and N should be binary 10bb where bb can be either for variant 1 of version 4 of the UUID specification.

I went ahead and edited your UUID generator function so it's compliant (it now generates correct uuids, but i haven't tested it with google analytics):

Code: Select all

function analytics.createUUID()
math.randomseed(os.time())
local uuid = {}
local char = {[0] = '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
for i = 1, 32+4 do
uuid[i] = char[math.random( 0x0 , 0xF )]
end
uuid[9], uuid[14], uuid[19], uuid[24] = '-', '-', '-', '-'
uuid[15] = '4'
uuid[20] = char[math.random( 0x8 , 0xB )]
return table.concat(uuid)
end

Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

dusoft
Party member
Posts: 111
Joined: Fri Nov 08, 2013 12:07 am

### Re: Google Analytics library (using Measurement protocol)

Thanks, it does not really matter as clientID can be anything really (tested), but you are right I should follow the recommendation. Thanks, I will replace this function.
zorg wrote:
Thu Oct 24, 2019 11:22 pm
One thing, this page says that the uuid function should be type 4 according to the linked rfi:
"The value of this field should be a random UUID (version 4) as described in http://www.ietf.org/rfc/rfc4122.txt."

https://en.wikipedia.org/wiki/Universal ... 4_(random)
According to the above link, the implementation of the UUID should have this signature to be correct:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
where M is the hex digit 4 and N should be binary 10bb where bb can be either for variant 1 of version 4 of the UUID specification.

I went ahead and edited your UUID generator function so it's compliant (it now generates correct uuids, but i haven't tested it with google analytics):

Code: Select all

function analytics.createUUID()
math.randomseed(os.time())
local uuid = {}
local char = {[0] = '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
for i = 1, 32+4 do
uuid[i] = char[math.random( 0x0 , 0xF )]
end
uuid[9], uuid[14], uuid[19], uuid[24] = '-', '-', '-', '-'
uuid[15] = '4'
uuid[20] = char[math.random( 0x8 , 0xB )]
return table.concat(uuid)
end


pgimeno
Party member
Posts: 2408
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Google Analytics library (using Measurement protocol)

The above function has two problems:

- If generating two UUIDs in the same second, it will return the same value.
- Two people generating UUIDs at the same time will get the same value.

For the first problem, it's better to assume that the random seed has been set beforehand. For the second problem, it's better to use LÖVE's love.math.newRandomGenerator and the fractional part of love.timer.getTime(), which reduces the chance of getting the same value substantially.

While on it, the two tables (uuid and char) could be placed out of the function, so that they don't cause reallocation and garbage collection on every call.

Suggested code:

Code: Select all

local uuid = {}
local char = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
local rng
do
local time = love.timer.getTime()
local fraction = time % 1
rng = love.math.newRandomGenerator(math.floor(fraction * 0x100000000), time - fraction + os.time())
end

function analytics.createUUID()
for i = 1, 32+4 do
uuid[i] = char[rng:random(0x0 + 1, 0xF + 1)]
end
uuid[9], uuid[14], uuid[19], uuid[24] = '-', '-', '-', '-'
uuid[15] = '4'
uuid[20] = char[rng:random(0x8 + 1, 0xB + 1)]
return table.concat(uuid)
end


dusoft
Party member
Posts: 111
Joined: Fri Nov 08, 2013 12:07 am

### Re: Google Analytics library (using Measurement protocol)

Not really a problem from the Analytics point of view, though.
1) You only need to generate UUID once and then store it (for each user). If you expect high volume, you can prepend UUID with a custom value (clientID can be any string, even if a proper UUID is recommended).
2) Two people won't use the same Analytics ID (property), so they will send hits to their respective properties (that are separate).

But thanks for pointing this out, I am going to rewrite it to be more randomized as you suggest.
pgimeno wrote:
Fri Oct 25, 2019 9:55 am
The above function has two problems:

- If generating two UUIDs in the same second, it will return the same value.
- Two people generating UUIDs at the same time will get the same value.

### Who is online

Users browsing this forum: No registered users and 35 guests