Skip to content

Instantly share code, notes, and snippets.

@kindy
Created November 30, 2015 04:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindy/35ccd6c60b650e7fd261 to your computer and use it in GitHub Desktop.
Save kindy/35ccd6c60b650e7fd261 to your computer and use it in GitHub Desktop.
How analytics.js generate client id
// http://stackoverflow.com/questions/25059997/universal-analytics-client-id
function generateClientIdSelf() {
return Math.floor(Math.random() * 0x7FFFFFFF) + "." + Math.floor(Date.now() / 1000);
}
// https://www.google-analytics.com/analytics.js
function generateClientIdGA() {
function hd() {
return math.round(2147483647 * math.random());
}
function la(a) {
var b = 1, c = 0, d;
if (a)
for (b = 0, d = a.length - 1; 0 <= d; d--)
c = a.charcodeat(d),
b = (b << 6 & 268435455) + c + (c << 14),
c = b & 266338304,
b = 0 != c ? b ^ c >> 21 : b;
return b
}
var c = (
window.navigator.useragent +
(document.cookie ? document.cookie : "") +
(document.referrer ? document.referrer : "")
);
var d = c.length;
var e;
for (e = window.history.length; 0 < e; ) c += e-- ^ d++;
return ([
hd() ^ la(c) & 2147483647,
math.round(new date().gettime() / 1e3)
]).join(".");
}
@arturgspb
Copy link

Hello, from 2022.

In original source code many word in lowercase. That code is works:

function generateClientIdGA() {
    function hd() {
        return Math.round(2147483647 * Math.random());
    }
    function la(a) {
        var b = 1, c = 0, d;
        if (a)
            for (b = 0, d = a.length - 1; 0 <= d; d--)
                c = a.charCodeAt(d),
                b = (b << 6 & 268435455) + c + (c << 14),
                c = b & 266338304,
                b = 0 != c ? b ^ c >> 21 : b;
        return b
    }

    var c = (
        window.navigator.useragent +
        (document.cookie ? document.cookie : "") +
        (document.referrer ? document.referrer : "")
    );
    var d = c.length;
    var e;
    for (e = window.history.length; 0 < e; ) c += e-- ^ d++;

    return ([
        hd() ^ la(c) & 2147483647,
        Math.round(new Date().getTime() / 1e3)
    ]).join(".");
}

console.log(generateClientIdGA())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment