Blast Analytics and Marketing

Analytics Blog

Supporting Leaders to EVOLVE
Category: Digital Analytics

Using Multiple Trackers in Google Analytics

April 17, 2013

Why would you want to use multiple trackers?

In case you didn’t know, there are ways to put tracking code on pages that will track to two or more Google Analytics accounts or web properties. It can get kind of tricky but once you understand the concept, it’s a breeze.

Let’s start with having a reason to put multiple trackers on your site’s pages. I’ll give you two:

  1. Tracking for affiliates – Suppose you have an e-commerce site and the vendor you use for transactions requires tracking transaction information to their own Google Analytics account.
  2. Your company has multiple sites – Perhaps your company has several locations, each with its own domain, and you want both an individual look at each domain and an aggregate look for all domains combined. This is referred to as a “roll-up tracker.” A roll-up tracker could be Profile based, Property based or even Account based. Each domain would then track on its own plus to the roll-up.

What if you have multiple sites AND also track for affiliates, would you need to have 3 trackers on some pages? YES

Is it possible to track to more than two Google accounts or web properties? the good news is YES!

Do I need a Profile, Property or Account Rollup?

Account Roll-up:
In the case of an affiliate, the need for a separate tracker is more like a requirement. The affiliate company will be tracking to their account and your site tracks to your own account.

Property Roll-up:
A Property roll-up as opposed to an Account based roll-up offers the advantage that since all Properties within an Account share users, filters and data sources, it’s much easier to configure these items and assign them to the appropriate Properties without the need to create them multiple times.

Having each site on its own Property allows you to easily report only on that specific site without the need for filtering out traffic outside of the specified domain or sub-domain. This comes really handy when you have a large collection of sites. Imagine having to create Include and Exclude filters for 24 sites?

However, this advantage presents the disadvantage of not being able to report aggregate data. That’s where a roll-up account comes in. Implementing multi-tracking code on the sites to track to separate Properties, will let you see aggregated traffic on your roll-up Property while maintaining the singularity of your site’s own Property. Add a filter to show the full domain on your roll-up Property and now you’re able to see traffic to all sites within your content reports. Add cross domain tracking to your roll-up account and you’re able to see the flow of visitors across the sites.

Profile Roll-up:
Profile roll-ups don’t require multi-tracking code, simply because profiles share their parent Property ID so the code would be the same on all sites.

If you have 2 or 3 domains, setting up a Profile roll-up may be the best solution since you don’t have to change your code and handling filters is not that complicated. Creating multiple profiles is a lot easier than creating multiple Properties since copying a profile also copies assigned filters, goals and settings while there’s no option to copy a Property. Your roll-up Profile would still need to have a filter to show the full domain.

Now that we know what we want, how do we do it?

Although it can get a little tricky when you start thinking about cookie sharing and such, the key to the solution is namespacing your trackers. This presents a little more work for your development team and unfortunately introduces room for error.

Multiple Trackers, The Standard Method

Google provides some brief guidelines into how the code needs to be structured in the “One Push, Multiple Commands” section of their development guidelines – however, they don’t really tell you much so here’s a good example:

[sourcecode language="js"]
_gaq.push(
  ['_setAccount', 'UA-XXXXX-1'],
  ['_trackPageview'],
  ['rollup._setAccount', 'UA-XXXXX-2'],
  ['rollup._trackPageview']
  ['affiliate._setAccount', 'UA-YYYYYY-1'],
  ['affiliate._trackPageview']
);
[/sourcecode]

The formula is simple, add a prefix to the Google Analytics command for your additional trackers and duplicate your code for every tracker you need. If you put two and two together, you see how this can be tedious work. You would need to duplicate every _gaq.push you have throughout your site. All Events, Pageviews, Custom Variables, and any other standard tracking commands but worst of them all, your e-commerce code complete with every _addItem command listed. That’s where it really gets heavy.

Multiple Trackers, The Blast Method

We believe in simplicity and automation. Eliminating development time and lowering the potential for errors is what drove us to develop this multi-tracker script. The benefits of the script include:

  • Easy to configure web property numbers and tracker names
  • Automated setAccount for all trackers
  • Ease of use by maintaining current method structure
  • Option to specify tracking on only specific trackers
  • Ability to modify how data is sent to Google

The Script

To start, we’re going to set some array variables at the beginning of your code:

[sourcecode language="js"]
var GA_Account_IDs = ['UA-XXXXXX-1','UA-XXXXXX-2','UA-YYYYYY-1'];
var GA_Tracker_Names = ['main','rollup','affiliate'];
var GA_Trackers = [];
[/sourcecode]

You’ll want to enter the actual web property numbers for GA_Account_IDs. For the tracker names, we’ve set a pretty accurate default set of names. Feel free to change them as you please. The values can be either hard coded or populated servers side. In the example of an affiliate solution, you may be interested in capturing e-commerce transaction pages but not necessarily every page of your site. A server-side script could determine whether or not to populate the affiliate information. Make sure that you always match the position of the UA# with the position of the Name. For example, if you didn’t want to track to the roll-up on a specific page, you would enter the first and third UA#s along with “main” and “affiliate” names in the same order.

The following configuration is the star of the show:

[sourcecode language="js"]
var GA_Track = function() {
  // check to see if list of trackers is set, otherwise set to blank
  var trackers = (typeof(GA_Tracker_Names) === 'undefined') ? ['']: GA_Tracker_Names;
  // grab function arguments and put into array args
  var args=[].slice.call(arguments), c = [], p = [], d;
  // check for 'bam' for specifying tracker names
  if(args[0][0]==='bam'){
    // if 'bam' is the first item of the first argument, grab the supplied tracker names and put into array trks
    c.trks=(trackers.length>1) ? args[0].splice(1): trackers;
    // drop first argument and put the rest into args
    c.args=args.splice(1);
  } else { // if no 'bam' is present, use trackers and args as supplied
    c.trks=trackers;
    c.args=args;
  }
  for (d=0; d<c.trks.length; d++) { p[d] = [];
    for (var a=0; a<c.args.length; a++) { p[d][a] = [];
      for ( var i=0; i<c.args[a].length; i++ ) { p[d][a][i] = [];
        // if not main tracker and if first argument, insert tracker name, otherwise if main tracker leave blank, if not first argument, skip
        var r = ( i != 0 ) ? '_': ( c.trks[d] == trackers[0] ) ? '': c.trks[d]+'.';
        p[d][a][i] = (r!='_') ? r+c.args[a][i] : c.args[a][i];
      }
      // console.log( p[d][a] ); // debug:
      _gaq.push( p[d][a] ); // push to _gaq object for GA only
    }
  }
};
// initiates the GA accounts and populates the Trackers
for ( var id=0; id < GA_Account_IDs.length; id++ ) {
  GA_Trackers.push(GA_Tracker_Names[id]);
  GA_Track(['bam',GA_Trackers[id]],['_setAccount', GA_Account_IDs[id]]);
}

[/sourcecode]

More on the script will follow, but we have one more step before the task is done.

Code Replacements

As Google officially starts introducing Universal Analytics, moving forward we need to take steps to minimize the amount of work for future migrations. Since Universal Analytics uses a different syntax than Google Analytics, a migration to Universal Analytics would require you to change all existing tracking code and adjust it to the new syntax. However, Universal Analytics is a developing technology and not all existing features of Google Analytics (GA) are fully supported. So the best solution is to implement tracking code via a function that will collect the necessary data throughout the site and then reformat the way the data is sent to Google within that function.

The basis of the script is to replace the _gaq.push method with the GA_Track function on the initial Google Analytics code implementation. Right at the very end of the GA_track function, you determine how data is formatted and sent to Google. A sweet bonus is that you can enable the debug console.log and you’ll see exactly what is getting sent to Google.

On existing implementations, we will take the first step towards a smooth Universal Analytics migration now by changing all the push methods on the site as follows:

Example code case:

[sourcecode language="js"]
_gaq.push(['_setCustomVar',1,'Customer Type','member',1],['_trackPageview']);
_gaq.push(['_addTrans',
   '1234',           // transaction ID - required
   'Womens Apparel', // affiliation or store name
   '28.28',          // total - required
   '1.29',           // tax
   '15.00',          // shipping
   'San Jose',       // city
   'California',     // state or province
   'USA'             // country
]);
_gaq.push(['_addItem',
   '1234',           // transaction ID - necessary to associate item with transaction
   'DD44',           // SKU/code - required
   'T-Shirt',        // product name
   'Olive Medium',   // category or variation
   '11.99',          // unit price - required
   '1'               // quantity - required
]);
_gaq.push(['_trackTrans']);
_gaq.push(['_trackEvent','page-interaction','cart confirmation','print page', 0, true]);
[/sourcecode]

With the standard method, you would need to duplicate the code above for each of the trackers. On a site with a roll-up tracker and an affiliate tracker, the code would need to be placed 3 times as shown on the previous example.

Instead, all you need to do is replace _gaq.push with GA_Track as shown below: (you’ll thank us later!)

[sourcecode language="js"]
GA_Track(['_setCustomVar',1,'Customer Type','member',1],['_trackPageview']);
GA_Track(['_addTrans',
   '1234',           // transaction ID - required
   'Womens Apparel', // affiliation or store name
   '28.28',          // total - required
   '1.29',           // tax
   '15.00',          // shipping
   'San Jose',       // city
   'California',     // state or province
   'USA'             // country
]);
GA_Track(['_addItem',
   '1234',           // transaction ID - necessary to associate item with transaction
   'DD44',           // SKU/code - required
   'T-Shirt',        // product name
   'Olive Medium',   // category or variation
   '11.99',          // unit price - required
   '1'               // quantity - required
]);
GA_Track(['_trackTrans']);
GA_Track(['_trackEvent','page-interaction','cart confirmation','print page', 0, true]);
[/sourcecode]

Choosing Your Trackers

Using the example of tracking for an affiliate, you would only be interested in tracking transactional pages. The account tracker does not need to be present on all pages of your site. This would be easily handled by simply omitting the tracker UA# and “affiliate” name. However, on the completion page, you want to track if the user prints the page and the affiliate does not care about this data.

You would then make a function to listen for the “print” option, or a click on the “print” button and then fire off an event tracker. However, since there have been 3 items already defined on the page; doing a simple GA_Track() call would go to all three.

To send only to the “main” and “rollup” trackers, you’ll need add an initial array whose first item is “bam” and is followed by the names of the desired trackers. Be sure your names here match exactly how they were initialized.

[sourcecode language="js"]
GA_Track(["bam","main","rollup"],['_trackEvent','page-interaction','cart confirmation','print page', 0, true]);
[/sourcecode]

So what if we only have 1 tracker? No problem, just enter 1 UA# and 1 tracker name. The functionality remains exactly the same. The difference being that you can go from 1 tracker to 3 trackers by simply adding the additional numbers/names. You can also go from standard GA to Universal or both, from one location.

Limits

So exactly how many trackers can you send to simultaneously? Well, that all depends. It’s hard to imagine a scenario needing more than 4 trackers per site. We would love to hear from anyone who has needed to do so!

Limits are set by how many hits can be done in a burst. For regular Google Analytics, this is set to 10 and then it replenishes at a rate of 1 per second. In Universal Analytics, this limit is 20, replenishing at 2 per second. Anything above that will be ignored. Read more about that on Google’s Developers pages.

Keep in mind, these limits are set for hits, regardless of what type of hits they are. If you initialize your page with 3 trackers, you’ll start off with 3 pageviews. If you fire off an Event immediately after, you’ll have 6 hits. A burst is any number of hits sent within a second. If you have to send several hits to several trackers, you may need to add some delays.

Use this Free Script

Feel free to use our script on your next project. If you have any questions about implementation, simply post them as comments below. Be sure to share, like and +1 if you enjoyed this post.

Olaf Calderon
About the Author

Olaf is the Director of Implementation at Blast Analytics & Marketing. He and the implementation team, handle configuration, installation and training of analytics tools. He spends the majority of time developing tracking code and He loves the challenges that every project presents and is always finding more efficient ways for our clients to collect, read and analyze their data. His passion is developing tools, processes or methods to make everyone's life easier.

Olaf Calderon has written on the Blast Digital Customer Experience and Analytics Blog.