Created
September 20, 2012 16:12
-
-
Save amaxwell01/3756840 to your computer and use it in GitHub Desktop.
How to have 2 or more Google Analytics accounts and events on a single web page
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
<html> | |
<head> | |
<title>Test Multiple Google Analytics on a single page</title> | |
<script type="text/javascript"> | |
var _gaq = _gaq || []; | |
(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); | |
})(); | |
</script> | |
</head> | |
<body> | |
<a href="http://plus.google.com" target="_blank" onclick="sendAnalytics('ga1','trackevent', { name : 'button3', event : 'clicked' }); sendAnalytics('ga2','trackevent', { name : 'button3', event : 'clicked' } );">Google+</a> | |
<script> | |
/* | |
* parameters.name : Name of the event triggered / tracked | |
* parameters.event : Type of event triggered / tracked | |
*/ | |
var sendAnalytics = function( account, type, parameters ) { | |
switch( account ) { | |
case 'ga1': | |
_gaq.push(['_setAccount', 'UA-6740548-20']); | |
break; | |
case 'ga2': | |
_gaq.push(['_setAccount', 'UA-6740548-21']); | |
} | |
switch( type ) { | |
case 'pageview': | |
_gaq.push(['_trackPageview']); | |
break; | |
case 'trackevent': | |
_gaq.push(['_trackEvent', parameters.name, parameters.event]); | |
break; | |
default: | |
// don't default to anything | |
break; | |
} | |
} | |
sendAnalytics('ga1', 'pageview'); | |
sendAnalytics('ga2', 'pageview'); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment