Uncategorized

GTM Hack – Combining Lookup Tables in GTM to Serve the Right GA ID

Today I’ll share a cool way to combine multiples lookup tables to serve more complex “look up problems”, namely one with more than one type of input. Of course, any lookup table can map 10 hostnames to their Google Analytics ID, but we can’t map a combination of hostnames AND environment names to the right ID. I’ll explain the use case in detail further down.

Basic Look Up Tables

A lookup table is a great variable you can use in Google Tag Manager to basically map inputs to outputs. But really, it’s just a table.

One simple way to use a look up table to serve Google Analytics IDs is to have as an input the “hostname” and the following rows:

This way you only need one variable (the look up table) to serve a dozen tags you can use across different websites.

In my case, I’d like to look at something else than the hostname, but rather some custom “Website Language” which you might use if your website language is not differentiated by the hostname, but by some subdirectory. So my rows are something like the following two:

  • example.com/en => en (via “Website Language”) => UA-XXXXXXXX-1
  • example.com/fr => fr (via “Website Language”) => UA-XXXXXXXX-2

Combine with a Testing Environment

Now say we also have a testing environment. The testing environment mirrors the complete website, so there we also have those two “Website Languages” “en” and “fr”. What you’d probably like to do is to have a separate Google Analytics ID served to the testing environment. So what we’d really like to have is the following “table”:

  • Testing Env + example.com/en => UA-XXXXXXXX-9
  • Testing Env + example.com/fr => UA-XXXXXXXX-9 (I don’t mind having all the testing data together in one Google Analytics Property)
  • Live Env + example.com/en => UA-XXXXXXXX-1
  • Live Env + example.com/fr => UA-XXXXXXXX-2

Notice something? We now got two types of inputs, the “Website Language” and the Environment “Type”, and one kind of output. But GTM lookup tables only take one input.

But you can fix this by using two lookup tables after each other.

Environment Name Look Up Table

The first table is the one in front filtering for whether we are in the testing environment or in the live environment, here’s the setup:

If the environment name is “Live” we output the Website Language Version, if it’s something else or Staging, we output simply “Staging”.

Website Language Lookup Table

The second table now takes the first one as input and looks like this:

Thus in two steps, we get the desired output, with two inputs.

Of course, if we have three inputs, we could hook up another lookup table.

Beware of Drafts

One note, if you test this, be aware that in “preview” the environment name will be “Draft …” like in this picture:

 

So to actually test this, you’ll have to publish it (which is ok if you have an actual testing environment).

Wrap it into a Google Analytics Settings Variable

Finally don’t forget to plug this into your Google Analytics Settings Variable, to further reduce the amount of duplicate information in your system.

If you have a different solution to this problem or more complex applications of multiple lookup tables, drop me a comment!

Update: Related: Some time after publishing this I came across a post on so called “chaining up look up tables” which is what I do here. Might be worth checking out as well.

Leave a Reply

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