Created
April 2, 2021 13:44
-
-
Save lekoala/1374d993bad445174f445b2a7e306454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// IE support if needed | |
// @link https://caniuse.com/?search=math.imul | |
if (!Math.imul) { | |
Math.imul = function (opA, opB) { | |
opB |= 0; | |
var result = (opA & 0x003fffff) * opB; | |
if (opA & 0xffc00000) result += (opA & 0xffc00000) * opB | 0; | |
return result | 0; | |
}; | |
} | |
// fast hashes thanks to bryc | |
// @link https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript/7616484#7616484 | |
function cyrb53(str) { | |
var seed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | |
var h1 = 0xdeadbeef ^ seed, | |
h2 = 0x41c6ce57 ^ seed; | |
for (var i = 0, ch; i < str.length; i++) { | |
ch = str.charCodeAt(i); | |
h1 = Math.imul(h1 ^ ch, 2654435761); | |
h2 = Math.imul(h2 ^ ch, 1597334677); | |
} | |
h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909); | |
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909); | |
return 4294967296 * (2097151 & h2) + (h1 >>> 0); | |
} | |
function getIP(callback) { | |
var req = new XMLHttpRequest(); | |
req.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
callback(this.responseText) | |
} | |
}; | |
req.open("GET", "https://checkip.amazonaws.com/", true); | |
req.send(); | |
} | |
// getIP(function(clientIP) { | |
var clientIP = ""; // you can use {$_SERVER['REMOTE_ADDR']} in php here or use getIP with a callback function for static html | |
var validityInterval = Math.round(new Date() / 1000 / 3600 / 24 / 4); | |
var clientIDSource = clientIP + ";" + window.location.host + ";" + navigator.userAgent + ";" + navigator.language + ";" + validityInterval; | |
var clientIDHashed = cyrb53(clientIDSource).toString(16); | |
(function (i, s, o, g, r, a, m) { | |
i['GoogleAnalyticsObject'] = r; | |
i[r] = i[r] || function () { | |
(i[r].q = i[r].q || []).push(arguments); | |
}, i[r].l = 1 * new Date(); | |
a = s.createElement(o), m = s.getElementsByTagName(o)[0]; | |
a.async = 1; | |
a.src = g; | |
m.parentNode.insertBefore(a, m); | |
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); | |
ga('create', 'YOUR-GA-TRACKING-CODE', { | |
'storage': 'none', | |
'clientId': clientIDHashed | |
}); | |
ga('set', 'anonymizeIp', true); | |
ga('send', 'pageview'); | |
//}); | |
//uncomment this line if using getIP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment