Google Analytics property ID management with Google Tag Manager

If, like me, you handle enterprise customers with a significant number of websites (we’re talking hundreds, if not thousands), one of your worst nightmares is Google Analytics property ID Management.

In this post, I’ll share with you one method I use to manage the Google Analytics property ID lists in Google Tag Manager, using an adaptable lookup table system.

[EDIT: 18 May, 2016 – code supports regular expressions]

A quick recap about Google Analytics property IDs

Most of the time, you’ll use a single Google Analytics property ID, a.k.a. GAID or UAID, for one given site. The GAID determine in which silo your analytics data get sent.

It is crucial that you send your traffic data to the right property because once data is collected, there is no retroactive processing and Google Analytics view filters can only do so much.

The classic error is sending your live traffic data to your staging/development property ID because you forgot to switch the tracking ID before pushing to production. This usually results in empty reports until someone realizes something is wrong but it’s usually too late. The site launch data is wasted because the wrong tracking ID was used, the marketing team is furious and IT gets blamed. Same old, same old.

Sometimes, the GAID will change ever so slightly for a site section or a specific business unit or product division. As you can guess, multiplying the number of Google Analytics property IDs can make things more difficult but it’s not rocket science. You just need careful planning, usually involving tall Excel sheets.

Using a tag manager helps manage Google Analytics property IDs but even with Google Tag Manager, Adobe Tag Manager, Tealium, Ensighten, Tag Commander and sundry, managing IDs is not ideal.

The easy yet limited route: GTM lookup table

Google Analytics Property ID in Google Tag Manager - page hostnameBut let’s try and see how we can address this, using Google Tag Manager. In our case the steps I describe can be easily adapted to other tag managers but because you will be exposed to GTM, let’s go with that.

Let’s assume that we want to base our GAID selection based on website hostname (www.yoursite.com). We first need to enable the Page Hostname variable in GTM variables so go to Variables and check the “Page hostname” box.

There, we can now use this variable in other GTM elements such as tags, triggers or even other variables.

Next we want to create a new variable so stay in Variables and click the red New button under User Defined variables.

Select a Lookup Table variable and select the Page Hostname variable in the drop down menu:

Google Analytics Property ID in Google Tag Manager - lookup table 1

You’ll notice I also placed my variable in my Identifiers folder for easier retrieval later.

Next I get to select which values are going to match my hostnames, assuming I use the same GTM container on all sites. So let’s click Add Row for each new entry.

For my first site on www.mysite.com, I’m going to define a (fictitious) tracking ID of UA-123456789-1 and for my second site at www.myothersite.com, I’ll use property ID UA-123456789-2 (same account, different property).

Finally, I can use a fallback default value in case my hostname does not match the rows I entered.

Google Analytics Property ID in Google Tag Manager - lookup table 2

Now I can use this variable as the tracking ID in my next Google analytics tag:

Google Analytics Property ID in Google Tag Manager - lookup table 3

All right, that was easy but this method has PROs and CONs.

PROs:

  • works great for a limited number of sites
  • great for easy row entry
  • that’s about it 🙂

CONs:

  • rows are displayed in the order they were entered
  • not suitable for large number of rows
  • rows only test against the “equals” conditions,
    with no support for “contains” or regular expressions.

So what’s the alternative? Let’s use a little Javascript code.

Alternative: Using Google Analytics property IDs in a JavaScript array

If you too are used to managing lots of sites, chances are you’re juggling with an Excel sheet that lists all said sites as well as their attributes, including Google Analytics property IDs.

Google Analytics Property ID in Google Tag Manager - in Excel

Sounds familiar? In my case, I need this Excel sheet to work for me. At least better than by looking it up manually and inserting values into page tagging or in a tag manager.

As I hinted at before, GAIDs can be used for individual site sections, not just domain names or host names. Therefore, you want the Excel sheet to include a parameter to discriminate between GAIDs.

Here is what I suggest:

  1. export that Excel sheet as a CSV file, with a couple modifications
  2. use the CSV as a JavaScript array
  3. parse the array based on the discriminating parameter
  4. retrieve the appropriate GAID for the current site
  5. get page views
  6. profit!

Let’s get to work!

Ideally, the file format should be as follows:

discriminator|parameter string|GAID

Example:

dns|crepemaster.info|UA-224661122-1
url|mysite.com/blog|UA-123456789-1
url|mysite.com/store|UA-123456789-2

On the example above, I have 3 GAIDs to handle:

  • One for fictitious domain name crepemaster.info and assign it GAID UA-224661122-1.
  • One for mysite.com but only for the blog section of the site
  • Another one for mysite.com but only for the store

Notice how I use the pipe sign as a separator but any standard separator (comma, semi-colon) will do.

Now let’s use that CSV in an array as mentioned.

Let’s go to GTM and create a new Custom Javascript User Defined variable:

Google Analytics Property ID in Google Tag Manager - variable array 1

Then use the following code:

[code lang=”javascript”]
function() {
var _dlh = document.location.hostname;
var _dlp = document.location.href;
var gaID = "UA-123456789-1"; // default
var csvSites = [ // paste parameter/domain/UA combos from CSV here
‘dns;crepemaster.info;UA-224661122-1’,
‘url;mysite.com/blog;UA-123456789-1’,
‘url;mysite.com/store;UA-123456789-2’,
‘rxd;mozilla.org|siteb.com;UA-123456789-3’,
‘rxh;/folder[0-9]{1,3};UA-123456789-4’
];

var i = 0;
while (i < csvSites.length) { // go through the array
var _ld = csvSites[i];
var _el = _ld.split(‘;’); // break each row down using the separator
switch (_el[0]) { // now use the discriminator to determine how to process the string
case ‘dns’:
var _match = _dlh;
if (_match.indexOf(_el[1]) > -1) gaID = _el[2];
break;
case ‘url’:
var _match = _dlp;
if (_match.indexOf(_el[1]) > -1) gaID = _el[2];
break;
case ‘rxd’:
var _match = _dlh;
var _r = new RegExp(_el[1]);
if (_r.test(_match)) gaID = _el[2];
break;
case ‘rxh’:
var _match = _dlp;
var _r = new RegExp(_el[1]);
if (_r.test(_match)) gaID = _el[2];
break;
}
i++;
}
return gaID;
};

[/code]

As I’m sure you have guessed by now, you can modify the discrimination mechanism, to look for a data layer variable, for instance.

EDIT: I extended the above code to account for regular expressions on domain name (rxd) or URL (rxh). Enjoy! 😉

If you can’t manage the array in GTM itself (adding more array entries may push the container over the size quota), you can also link to an external resource such as a Google Sheet or a (g)zipped .js file.

Now that you have your new variable containing your desired Google Analytics property ID, you can feel free to use it in Google Analytics tags!

In conclusion

So here is a trick I like to use to go through a large number of Google Analytics property IDs using Google Tag Manager. Using this technique along with Hub’Scan for digital analytics quality assurance is a sure fire way of getting clean data in your favorite analytics solution 😉

How about you? How do you handle large number of Google Analytics property IDs?
Let me know in the comments!

Author: Julien Coquet

Expert de la mesure d’audience sur Internet depuis plus de 15 ans, Julien Coquet est consultant senior digital analytics et responsable produit et évangélisation pour Hub’Scan, une solution d’assurance qualité du marquage analytics. > A propos de Julien Coquet

2 thoughts on “Google Analytics property ID management with Google Tag Manager”

    1. My apologies, in my question above, I should have written “UA -” instead, regarding the measurement id.

      Thanks and best wishes,
      Nancy

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.