A client requested a Rollup account to aggregate all the stats into one property from multiple sites. The general context was that each region (e.g. UK, Spain, France, Australia, South Africa, Canada, US etc) were given a regionalized page (each with a unique property ID in GA) and all general metrics as well as Custom Event tracking for all assets will be “rolled-up”/aggregated into one main property to make reporting easier.
A diagram of the general concept is below:
I don’t want to go into the basics (property setup) as there is nothing really unique about the actual administration of the account, but rather, I want to focus more on a working example of the JS code required to get this to work with GA’s Universal Analytics code.
But as starters, my “Landing Page Account” (i.e. the general account) contained the following properties (I’ve cut the number down to 2 properties for simplicity sake, but I had 13 regions pushing to the rollup when it was live):
- Main Rollup Property – (UA-XXXXXXXX-1)
- UK’s Page Property – (UA-XXXXXXXX-2)
- Canada’s Page Property – (UA-XXXXXXXX-3)
Please note that the rollup property had a fictitious URL in the “Default URL” in my property settings (the Rollup property, in actuality, is virtual and has no URL as it just aggregates data). This did not affect data collection at all.
What I need to do – General Tracking
The key to tracking multiple properties with the same GA snippet is to create a second tracker and assign a new name to reference it (I called this tracker “t_global”). Once the reference is created, you can then call functions with this tracker name (Line 10).
As you can see, the following code was posted on the “UK” website. On Line 8, it just associates the default tracker with “UA-XXXXXXXX-2” (UK’s unique GA property ID) and assigns the Rollup property “UA-XXXXXXXX-1” (the rollup’s GA property ID) to a tracker named “t_global” (Line 7).
- <script>
- (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
- m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
- })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
-
- ga('create', 'UA-XXXXXXXX-1', {'name': 't_global'});
- ga('create', 'UA-XXXXXXXX-2');
-
- ga('t_global.send', 'pageview');
- ga('send', 'pageview');
-
- </script>
That’s it for one property. If we wanted to add Canada’s site to the rollup we’d change Line 8 to “UA-XXXXXXXX-3” (Canada’s unique GA property ID)
From there the sites should be sending data in to their regional property as well as the aggregated rollup account.
What I need to do – Event Tracking
My actual implementation on the live sites was far more complex and I had to create a custom library to manage all the content, links and assets, but the general idea around how we’d implement event tracking is below.
There is nothing really special beyond using the default and “t_global” tracker references to call the typical event tracking functions, but to make it a bit more concrete. If we wanted to track a PDF that opened up in a new window we’d have the following code:
Sample HTML link on a page:
JavaScript function that will call GA’s event tracking function:
- <script>
- function trackEvent(category, action, label){
- ga('send', 'event', category, action, label);
- ga('t_global.send', 'event', category, action, label);
- }
- </script>
It’s as easy as that. I’d recommend attaching these events programmatically (maybe with the help of jQuery or your favorite JS framework or selector engine to get the job done) and also doing some checks to ensure that GA is loaded before calling these functions. Nevertheless, at heart, all that is needed is a reference to the “t_global” tracker as well as the default one and call both function to send the corresponding data to each property.
The previous JS snippet can be placed on every regional site without having the need to be modified. This is because the GA code is already customized for each region and rollup property (i.e. has the unique ID assigned to the default tracker and the rollup property ID attached to “t_global” – recall the UK and Canada snippets we produced earlier).
References: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#name and https://developers.google.com/analytics/devguides/collection/analyticsjs/events