8

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

Jame H
  • 1,324
  • 4
  • 15
  • 26

1 Answers1

8

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>
Martin Meixger
  • 2,547
  • 24
  • 26
  • 1
    Thks @Martin Meixger, I found this code from anayltic.js after a long time. But thank you for your answer, I think nobody will help me. But I have a little question about the ability of global unique random ID. How they can ensure about their ID. It still a small chance to duplicate, right? – Jame H Dec 01 '17 at 01:37
  • @JameH The full clientId is a combination of the 3rd and 4th element; which is a timestamp. The combination of the two is really highly unlikely to repeat. It would require the same unique ID being generated at the same time. You should be using the last two elements together to identify users or for reporting. – Muhammad Ali Feb 04 '22 at 15:43