Forked from DanWilkerson/gist:a90684b2491947ce31b61809421a8dc8
Created
September 7, 2021 05:02
-
-
Save userbox020/6d4626b9725c155c8b7a3a711e5d5015 to your computer and use it in GitHub Desktop.
Google Analytics Client ID Generator
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
/** | |
* Generates a client ID in the format historically used by the Google Analytics | |
* JavaScript libraries. Note that any alphanumeric value may be used, but | |
* ideally each should be unique to a given client. | |
* | |
* More information on Client IDs: | |
* https://developers.google.com/analytics/devguides/collection/protocol/v1/email#client-id-cid | |
*/ | |
function generateGaClientId() { | |
var ts = Math.round(+new Date() / 1000.0); | |
var rand; | |
try{ | |
var uu32 = new Uint32Array(1); | |
rand = crypto.getRandomValues(uu32)[0]; | |
} catch(e) { | |
rand = Math.round(Math.random() * 2147483647); | |
} | |
return [rand, ts].join('.'); | |
} | |
/** | |
* Tested on: | |
* - IE6+ | |
* - Firefox 3.6 & 49 | |
* - Opera 40 | |
* - Chrome 53 | |
* - Safari 5.1 & 9 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment