Using Multiple Google Analytics Trackers

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.

About Devin

I'm a WordPress developer based in Austin, Texas. Follow my projects on GitHub, or more general WordPress ramblings as @devinsays on twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>