Created
March 21, 2016 12:23
-
-
Save OrenBochman/68cf808f77c7efbc32eb to your computer and use it in GitHub Desktop.
highly customised universal analytics snippet with cross domain, cross device and autotracking support
This file contains hidden or 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
<!-- Universal Analytics start --> | |
<script async src='path/to/autotrack.js'></script> | |
<script> | |
var userID = "123"; // (set the user ID here via php etc.) | |
function getClientID() { | |
return ga(function(tracker) { | |
var clientId = tracker.get('clientId'); | |
}); | |
} | |
function genSessionID() { | |
return new Date().getTime() + '.' + Math.random().toString(36).substring(5); | |
} | |
function genTimeStamp() { | |
// Get local time as ISO string with offset at the end | |
var now = new Date(); | |
var tzo = -now.getTimezoneOffset(); | |
var dif = tzo >= 0 ? '+' : '-'; | |
var pad = function(num) { | |
var norm = Math.abs(Math.floor(num)); | |
return (norm < 10 ? '0' : '') + norm; | |
}; | |
return now.getFullYear() | |
+ '-' + pad(now.getMonth()+1) | |
+ '-' + pad(now.getDate()) | |
+ 'T' + pad(now.getHours()) | |
+ ':' + pad(now.getMinutes()) | |
+ ':' + pad(now.getSeconds()) | |
+ '.' + pad(now.getMilliseconds()) | |
+ dif + pad(tzo / 60) | |
+ ':' + pad(tzo % 60); | |
} | |
(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', 'UA-XXXXXX-YY', 'auto', { //create tracker + configuration objects | |
'allowLinker': true , 'userId': userID // cross domain && cross device user_id trackin | |
}); | |
//plugins | |
ga('require', 'displayfeatures'); // doubleclick cookie AKA display feature | |
ga('require', 'linker'); // cross domain plugin | |
ga('require', 'linkid'); // enhanced link attribution plugin | |
ga('require', 'autotrack'); // autotraking plugins | |
ga('linker:autoLink', ['main.domain.com', 'otherdomain.co.il', 'domain.com']); | |
//custom dimension to complete schema | |
ga('set', { 'dimension0': userID}); //set user ID (user scope var) | |
ga('set', { 'dimension1': getClientID() }); //set clientID (session scope var) | |
ga('set', { 'dimension2': genSessionID() }); //set sessionID (session scope var) | |
ga('set', { 'dimension3': genTimeStamp() }); //set time stamp (hit scope var) | |
//ga('require', 'ecommerce'); // ecommerce or | |
//ga('require', 'ec'); // enhanced ecommerce | |
//set content groups | |
if (document.location.pathname.indexOf('user/' + userID) > -1) { //remove userID from page to reduce cardinality | |
var page = document.location.pathname.replace('user/' + userID, 'user'); | |
ga('send', 'pageview', page); | |
}else{ | |
ga('send', 'pageview'); //send a hit | |
} | |
//send impressions + promotions | |
</script> | |
<!-- Universal Analytics end --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
based in parts on: