Skip to content

Instantly share code, notes, and snippets.

@craig552uk
Last active December 20, 2015 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craig552uk/6103720 to your computer and use it in GitHub Desktop.
Save craig552uk/6103720 to your computer and use it in GitHub Desktop.
Blog: Extract the Google Analytics Visitor ID from the cookie and push it back to GA as a custom variable.
// Extracts the Visitor ID from a GA cookie
function _goog_id(){
var id, a, c = document.cookie.split('; ');
for(i in c){a = c[i].split('=');if(a[0]=='__utma'){id = a[1].split('.')[1];}}
return id || 'unknown';
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(function(){_gaq.push(['_setCustomVar',5,'VisitorID',_goog_id(),1]);});
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
@digdeep
Copy link

digdeep commented Apr 13, 2014

neat code, I have updated my blog post with reference to your version (much cleaner than my version)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment