8 Apr 2016
Multiple Google Analytics trackers with Tag Manager
Reading time: 13 mins.
|
Difficulty:

Multiple Google Analytics trackers with Tag Manager


At the first Tag Manager Course in Madrid, in front of practically the entire staff of Havas Media, my friend Javi Martín (a fantastic digital analyst, originally from Madrid but now based in London), asked the following question:

“Can data be sent to various Analytics properties with the same GTM tag?”

Sometimes it’s necessary to go to the most primary of all sources of information: namely, the all-seeing, all-knowing Google! My thanks go out to this post by Krista Seiden, an evangelical Google trainer specializing in Analytics and Tag Manager, which helped clarify the matter; to my colleague Aitor Garcia, who helped me with the more technical aspects; and to Simo Ahava, for giving us the technical solution. This one’s for you, Javi! 😛
Solved! multiples trackings analytics
We’re going to look at how to perform multiple tracking in Google Analytics: in other words, how to send information to various Google Analytics properties from the same Google Tag Manager tag.

For a start, there’s the technical complexity of executing such an operation, and on top of that, we’d like to clarify once and for all what steps need to be taken in order to perform picture-perfect double tracking on a website (and without making that Google Tag Assistant alert appear, among other things).

When should I add multiple trackers to a website and send data to various GA properties?

We may sometimes wish to do this if, for example, we have several interlinked websites and we want standard, separate tracking on its own GA property for each in order to evaluate each domain in detail (and without sampling) as well as multi-domain tracking on a global rollup property that includes all of the domains we want to track as a whole. According to Google’s own documentation: “In some cases you might want to send data to multiple properties from a single page. This is useful for sites that have multiple owners overseeing sections of a site; each owner could view their own property.”

Multiple tracking has certain advantages compared to single, multi-domain tracking (without individual properties), such as:

  • Most importantly, reducing/minimizing the problem of sampling when generating separate properties. When we’re dealing with sites with a certain volume of traffic, and if we also concentrate a number of different domains in the same one property, we stand a much greater chance of reducing the quality and reliability of the data for the multi-domain account, given that it’ll have an increasing number of hits and therefore more chance of a higher rate of sampling. By keeping the properties separate, we can achieve more realistic data, if we need it. We may even be faced with the situation of having an enormous portal that exceeds Google Analytics’ compilation limits, in which case we can separate the site into sections and then do multi-domain operations with all of them. Maybe you’d be interested in this post: 5 ways to get unsampled data in Google Analytics.
  • An improved ability to configure each Google Analytics property. For example, the ratio for connecting Analytics to Search Console is 1:1, i.e. we can only set one domain for each GA property. In other words, if we have several domains in a multi-domain property, the least-bad option will be to choose one for Search Console and lose the rest of the data.

⇑ Article content (back to top) ⇑

Is it OK to include more than one Google Analytics tracking code?

At this point we need to get a little more technical. First of all: is it OK to do this? Can we include several Analytics trackers in the same website, i.e. more than one Google Analytics tracking code installed/inserted by Tag Manager?

In summary, Google’s official position is as follows: for Classic Analytics code (ga.js) it advises against it, but for Universal Analytics (analytics.js) it’s allowed, although the tracker has to be customized in accordance with its instructions.

Google’s recommended process consists of creating independent trackers so that the information is distributed correctly amongst the various Google Analytics properties. Otherwise, the hits could all be sent by mistake using the first tracker (the one that’s named after the Google Analytics default tracker). So how do we do this? Simply rename the tracker after one of the properties. We can see how to do it in the Google Analytics help section.

Working with multiple trackers

To track data for two separate properties, you need to create two separate trackers, and at least one of them must be a named tracker. The following two commands create a default tracker and a tracker named “clientTracker”:

ga('create', 'UA-XXXXX-Y', 'auto');
ga('create', 'UA-XXXXX-Z', 'auto', 'clientTracker');

Running commands for specific trackers

To run analytics.js commands for a specific tracker, you prefix the command name with the tracker name, followed by a dot. When you don’t specify a tracker name, the command is run on the default tracker.

To send pageviews for the above two trackers, you’d run the following two commands:

ga('send', 'pageview');
ga('clientTracker.send', 'pageview');

Source:
Working with multiple trackers in Google Analytics (official Google Analytics documentation)

 

We recommend doing it using GTM:

“The days of double tagging a website via code on your page are gone… Well, not really, you can still do it, but I wouldn’t recommend it. In fact, it’s never been a highly recommended practice for a many reasons, here are a couple:

  • Multiple instances of the classic GA tag (ga.js) can result in inaccurate data collection, processing, or reporting. If not setup properly multiple tags can cause competing calls to the cookie, causing the visitor ID to reset with every action on a page (this will 2x, 3x, or more over count you site data)
  • It’s harder to maintain because it’s 2 separate code blocks

What’s the solution then? Google Tag Manager! It’s actually really easy to setup GTM to fire to two analytics properties at the same time.”

Source: Bloggerchica.com (Krista Seiden)
The problem: the solution proposed (i.e. copy each UA-1 tag in GTM and duplicate it for UA-2) is great. Theoretically, it’s the most correct solution 🙂 However, this solution is not very efficient if we want to replicate full, complex tracking in GTM, with a multitude of tags such as pageview, virtual, events, etc. It would be too long-winded a process to duplicate all of the tracking, section by section.

Therefore, our aim is to make a single tag fire two or more times per hit, consecutively and to different Analytics properties. The only difference between the successive instances of the tag we’ve created will be certain key values: the name of the tracker, and of course the tracking UA. In principle, everything else will stay the same. So, now we more or less know what we want to do… but how do we do it?

⇑ Article content (Back to top) ⇑

Google Analytics trackers: technical notes

Because of the way Google Tag Manager works, every time a Google Analytics tag is triggered a unique tracker name is created automatically, along the lines of “GTM + Current timestamp”. This is absolutely fine for most cases, and you can find out more about it in this post by David Vallejo and this one by Simo Ahava. In summary: in the standard case, i.e. sending the data to a single property, there’s no problem and we don’t have to do anything, because GTM automatically assigns tracker names to the tags and it works perfectly; everything goes where it should.

IMPORTANT TECHNICAL NOTES:

Each tracker can only send values to a web property, and they usually have “global” configuration values associated with them.

>> Two Google Analytics tags with the same tracker name will share the configuration of these values.  

>> In contrast, tags with different tracker names can send data to the same web property while having entirely separate configuration values. This is why we tend to produce duplicates using tags we’ve already created, and replicate all of their configurations as custom parameters (e.g. none of the tags we create inherit).

If we want them to share or inherit values, we can force the tags to share the tracker name, but BE CAREFUL! This is extremely convoluted, highly advanced stuff. Despite that, I’ve wanted to clarify this issue for a while now. If you’d like to explore this topic in full and with much more rigor, here’s an excellent post on the subject. 

So, in summary, it’s vital that you take care when setting this field if you’re not 100% sure about what you’re doing.

So, from what we’ve seen so far, the main practical uses of the Set Tracker Name field in Tag Manager are:

  1. Checked but left blank (to make on-site tracking code compatible with GTM tracking; another advanced area that’s very, very useful!).
  2. Filled with a shared name (to share basic configuration values. BE CAREFUL: this is a very delicate matter and you need to understand the internal logic of GTM perfectly in order to know which values are inherited, which aren’t, which need to be cleaned, etc. For now, and in the majority of cases, we recommend that you don’t do this).
  3. Filled with a lookup table that generates different names in each instance for the same tracker (to send the same GA tag to different properties, which is what we want to do).
  4. Default option: unckeched and blank. This is fine for the majority of cases. New trackers and names are generated automatically, which is the safest setting for standard Analytics tracking.

Different tracker names for several UA trackers with the same tag

As we’re talking about firing the same tag several times, we want to make sure the tracker names are different so it can be sent to two different properties (on the subsequent occasions that the same tag is triggered we’ll want the tracker name to be different). Consequently, we’ll need to use the following configuration field in Google Tag Manager. This is a delicate operation, to the extent that it’s actively discouraged:

“Rename tracker”

AT LEAST ONE TRACKER MUST HAVE A SET TRACKER NAME

Tracker name Google Analytics (Tag Manager) - Nombre del Objeto de seguimiento

⇑ Article content (Back to top) ⇑

How do I fire GA tags several times in a row?

(Idea from Simo Ahava)

Here’s the source and explanation (v1 GTM). In this post we’re going to summarize the process, explain it and make it simpler by adding our own notes and recommendations.

Understandably, the difficulty lies in firing the tag a second (or more) time, because at first we’ll fire it, as always, via the GTM default All Pages trigger (Pageview or gtm.js). To do so, we’ll use a Field to Set that’s available in the tag setup for Google Analytics but which we may not have come across: namely, Hit Callback.

What is the hitCallback field used for in Google Analytics?

“When sending a hit to the Measurement Protocol, trackers send all of the fields that are currently stored” (Read more in Google Analytics documentation).

“In addition, the IP address, user agent string, and initial page inspection analytics.js does when creating a new tracker is used to determine things like the following:

  • The geographic location of the user.
  • What browser and operating system are being used.
  • Screen size and whether Flash or Java is installed.
  • The referring site.” 

(Read more in Google Analytics documentation)

However, there’s much more data and many more fields that we can take into consideration if we want to improve the standard analytics.js tracker.

One of the fields we can set is the callback; specifically, a hitCallback: This is Javascript code to be executed after processing the Google Analytics hit.

To do this, we can set the field in Tag Manager as follows:

hitCallback Google Analytics vía GTM

How can we introduce a function here?

We can create a custom JavaScript variable with the following content and call it {{tag callback function}}.

function() {
  return function () {
    var maxRepeat = 2;
    window.gtm_tag_counter = window.gtm_tag_counter || 0;
    if(window.gtm_tag_counter < maxRepeat-1) {
        dataLayer.push({'event': 'tagCallback'});
        window.gtm_tag_counter += 1;
    }
  }
}

What does this function do? 

It creates a global JavaScript variable to count the number of times the tag has been fired (gtm_tag_counter).

It also sets a maximum number of repetitions (i.e. the number of times we want to fire the tag). In this instance, the maximum is two.

Finally, every time the tag is fired the hitCallback function does the following:

  1. If we HAVEN’T reached the maximum number of repetitions, it adds 1 to gtm_tag_counter (the counter we created). And most importantly, it sends a data layer with a custom event namedtagCallback”. We’ll use this event to trigger the tag on the additional occasions.
  2. If we HAVE reached the maximum, it does nothing.

We’ll set the hitCallback field with the above variable:

Tag callback function Google Analytics

In the example we give here, and in order to differentiate it a little from Simo’s post, we only want the tag to fire twice (that’s why we change the function and set a maximum of two):

  1. In Pageview (Trigger: All pages)
  2. Second time (Trigger: tagCallback custom event)

Custom event Tag Callback

Triggers GA tag 2 times

 

Next, we need to use a global JavaScript variable (i.e. a GTM variable of the type “JavaScript Variable”, not to be confused with Custom JavaScript) that incorporates the counter calculating the number of times the tag is fired:

Tag callback counter

We’ll use this counter for the last major step: RENAMING THE TRACKER and generating a lookup table (conditional) for the tracker name, plus another one for the Analytics ID, depending on which property the tag data needs to be sent to (and on which turn/event it’s executed).

Our aim is as follows:

  • If the counter has a value of 0 (0<limit-1 =2-1, 0<1): it’s sent to property 2. The -1 is because the counter starts at 0, so therefore if we have two instances, we’ll have a 0 and a 1, rather than a 1 and a 2.
  • If the counter has any other value (including “undefined” or “null” because it has yet to be defined): it’s sent to property 1.

To do this, we need to create the following lookup tables and configure the tag as follows:

GTM Lookup Table: tracker name counter tag Lookup table: ID analytics tracker name

Analytics Tag sent two times via tag manager

To wrap up, and as a “reward” for making it to the end 🙂 we’re sorry to inform you that currently this cannot be tested in Google Tag Manager’s Preview and Debug mode (the event that we push to data layer in {{tag callback function}} isn’t directly triggered in the console, so we can’t check the full process). To test it we have to publish it, and then make sure it’s OK by checking Tag Assistant (we can also try it with Recordings), GA Real Time, the browser console, etc.

⇑ Article content (Back to top) ⇑