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. I run a little theme shop called DevPress and work for a startup called Nano. Find me on twitter @devinsays.

1 Response

  1. Thanks for sharing that useful script. Nice job. It would be nice to have a control of that codes via WP Dashboard. Maybe you will make a plugin for that purpose.

Leave a Reply