Skip to content

Instantly share code, notes, and snippets.

@simonvreeman
Created May 5, 2020 11:50
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 simonvreeman/57ccb3780809141e97f191d492b8ff41 to your computer and use it in GitHub Desktop.
Save simonvreeman/57ccb3780809141e97f191d492b8ff41 to your computer and use it in GitHub Desktop.
// Generate random Client ID
function uuid() {
var uuid = ( [1e7]+-1e3+-4e3+-8e3+-1e11 ).replace( /[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) );
return uuid;
}
(function() {
var ua = navigator.userAgent || navigator.vendor || window.opera;
var clientID = uuid();
var trackingID = 'UA-XXXXXX-YY';
var url = 'https://www.google-analytics.com/collect';
var payload = '?v=1' +
'&t=pageview' +
'&tid=' + trackingID +
'&cid=' + clientID +
'&dl=' + document.location.href +
'&dt=' + document.title +
'&dr=' + document.referrer +
'&aip=1' +
'&ua=' + ua;
var xhr = new XMLHttpRequest();
xhr.open('GET', url + payload);
xhr.send();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment