Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricionr/2825704830c7a3d6ccd0bedc934470b9 to your computer and use it in GitHub Desktop.
Save mauricionr/2825704830c7a3d6ccd0bedc934470b9 to your computer and use it in GitHub Desktop.
Google Analytics Client ID Generator
/**
* 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