Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate third field in _ga and _gid?

I'm investigate too much on _gid and _ga. And as i know, the definition of them:

_ga: used to identify unique users and it expires after 2 years.

_gid: used to identify unique users and it expires after 24 hours.

Example:

_ga: GA1.3.292651669.1502954402 _gid: GA1.3.974792242.1509957189

From what are the values in _ga cookie?, I know the meaning of each filed in _ga. And it's the same for _gid.

But I don't know how to generate third field, random generated user ID(for _ga:292651669 and for _gid: 974792242).

I tried to delete both _ga & _gid and I get the new couple _ga: GA1.3.2097663971.1509959880 and _gid: GA1.3.1180999143.1509959880. The third fields of both are changed. So how can they generate and how google identify user by them. Assumption I open browser today with a couple of _gid and _ga, and tomorrow, I cleared all cookies, ga will create of new couplw (_gid,_ga).; It means I'm in today that different tomorrow's me.

Please help me.

Thanks & Best Regards,

Jame

like image 982
Jame H Avatar asked Nov 06 '17 09:11

Jame H


1 Answers

Here is the code from analytics.js release 2017-09-21 that generates the random id. It's a random value based on current time, userAgent, cookie, referrer and history.length from the window object.

var O = window;

// var M = document;
// Stack Overflow doesn't allow cookie access in sandbox, so lets fake it:
var M = [];
M.cookie = "cookieName=someValue";

var hd = function() {
    return Math.round(2147483647 * Math.random())
}

function La(a) {
    var b = 1, c;
    if (a)
        for (b = 0,
        c = a.length - 1; 0 <= c; c--) {
            var d = a.charCodeAt(c);
            b = (b << 6 & 268435455) + d + (d << 14);
            d = b & 266338304;
            b = 0 != d ? b ^ d >> 21 : b
        }
    return b
}

var ra = function() {
    for (var a = O.navigator.userAgent + (M.cookie ? M.cookie : "") + (M.referrer ? M.referrer : ""), b = a.length, c = O.history.length; 0 < c; )
        a += c-- ^ b++;
    return [hd() ^ La(a) & 2147483647, Math.round((new Date).getTime() / 1E3)].join(".")
}


document.getElementById('output').innerText = ra();
<h3>Output:</h3>
<pre id="output"></pre>
like image 69
Martin Meixger Avatar answered Oct 14 '22 04:10

Martin Meixger