Last active
December 20, 2015 08:49
-
-
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.
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
// 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); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
neat code, I have updated my blog post with reference to your version (much cleaner than my version)