Sometimes it’s necessary to use multiple Google Analytics trackers on the same site. If you’re doing this, never paste in both of the default tracking scripts that Google provides.
Instead, set up the tracking variables and just call the Google script once. Here’s what I’ve used:
<script>
var _gaq=[
['_setAccount', 'UA-1111111-1'],['_trackPageview'],['_trackPageLoadTime'],
['secondTracker._setAccount', 'UA-2222222-1'],['secondTracker._trackPageview'],['secondTracker._trackPageLoadTime']
];
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = '//www.google-analytics.com/ga.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
</script>
Notes:
- This is an optimized version of the GA script from Mathias Bynens.
- You can include more than two. Just copy the secondTracker code and repeat.
- The “secondTracker” label can be changed to anything you like.
- _trackPageLoadTime isn’t required, but _trackPageview is.
- The code should be placed right before the close of the body tag.