Google Analytics Multiple Accounts

Based on the Async Analytics Code

Quite often I have to work with websites that have multiple versions of Google Analytics code embedded in them. Each tracking for a different account that was probably set-up by different people over the life time of the website.

Almost always, the code is a mess! Old code mixed with new to try and get around the multiple tracking issue.

It took me a while to find a clean solution, so I have decided to share it and hopefully save others time and frustration. I've also written a small script (see end) to make it even easier to track multiple profiles.

The Analytics Code

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
  _gaq.push(['_trackPageview']);

  // Second tracker 
  _gaq.push(['secondTracker._setAccount','UA-YYYYYYYY-Y']); 
  _gaq.push(['secondTracker._trackPageview']);

  (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>

The only change to the original code is in bold. Just add those three lines for each additional account you need to track. The important thing is that you create a new namespace for each new tracker (highlighted in red).

And don't forget to use real account codes!

Tracking events and custom page hits

Like above, you would need to trigger the event or page hit for each tracker.

A custom page hit would look something like this

_gaq.push(['_trackPageview', '/Special-Page']); 
_gaq.push(['secondTracker._trackPageview', '/Special-Page']);
        

An event would need something like this:

_gaq.push(['_trackEvent', 'link', 'click', 'linkClicked']); 
_gaq.push(['secondTracker._trackEvent', 'link', 'click', 'linkClicked']);
        

Automating it - A Solution

On larger websites it could be very complex to make sure the duplicate code is always implemented correctly, especially for events.

To help solve this I have written a little JavaScript file to automate the process.

Include File: WsaGa.js

The script lets you register all the profiles you wish to track, then provides functions to let you call the GA functions across all the profiles. Here's an example:

<html>
<head>
    <script type="text/javascript" src="WsaGa.js"></script>
    <script type="text/javascript">
        // Standard Google Code
        var _gaq = _gaq || [];

        // Important: Register all the Profiles you wish to send tracking data to
        WsaGa.registerProfile('UA-XXXXXXXX-1');
        WsaGa.registerProfile('UA-XXXXXXXX-2');
        WsaGa.registerProfile('UA-XXXXXXXX-3');

        // Important: Sets up all the accounts
        WsaGa.setAccounts();

        // Required for cross domain tracking
        // WsaGa.setAllowLinker(true); 

        // May be required for cross domain tracking. should match the current domain
        // WsaGa.setDomainName('domain.com');

        // Track page speeds
        // WsaGa.trackPageLoadTime();

        // Important: The main page view tracking
        WsaGa.trackPageView(); 

        // Other options...
        // WsaGa.trackPageView('test');
        // WsaGa.trackEvent('testcat', 'testaction', 'testlabel');
        // WsaGa.push(['_trackPageView', 'testpush']);

        // Standard Google Code
        (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>
</body>
</html>

Tell me (and others) if you find it helpful. Tell no one if you have any issues. Just Kidding! Post any questions or issues here or if you prefer directly contact me.