This website uses cookies to improve your experience and deliver relevant information.

on Sunday, 02 July 2017

How to use a lookup table in Google Tag Manager (GTM) to select the right Google Analytics property

Using a lookup table to render the correct Google Analytics property based on the environment or hostname was one of the great tips I picked up from the Google Tag Manager training at Brighton SEO.  It is pretty simple to set up, and once you have done it you'll never look back!


The prerequisites for this tutorial are the following:

Assuming you have all of the above completed, let's dive right in!

What is a lookup table

A lookup table is a type of variable in Google Tag Manager, which allows you to use conditional logic to say 'if this condition is true, do this; if that condition is true, do that; otherwise, do this'.

Checking your available variables

To get started, open up your Google Tag Manager interface, and click on 'Variables'. If this is the first time you've popped into this section, it is likely that you might not have all the available variables enabled.  Click on the 'configure' button next to the 'Built-In Variables' section, and tick the boxes next to the pages, utilities, errors, clicks, and forms - these are the ones you are most likely to need.

Creating a lookup table

While in the Variables section, scroll down to the bottom of the page, and when you get to the 'User-Defined Variables' section, click on 'New' to create a new variable.

When the next screen opens, click in the middle of the 'Variable Configuration' section to select what type of variable you will be using.

Scroll down to the 'Utilities' section, and select 'Lookup Table' from the available options.

google tag manager select lookup table

Choosing the correct Google Analytics property depending on environment or hostname

There are two common ways to achieve the expected outcome - you can either use the input variable of {{Environment Name}} or you can use the input variable of {{Page Hostname}}.  It is up to you which you choose, and there are operational reasons why you might prefer one over the other.

Whichever you choose, you enter the relevant environment name or page hostname (fully qualified domain name) in the first column, and the appropriate Google Analytics property ID in the second.  Repeat this for all the different environments you need, and for the live environment, check the box for 'Set Default Value' and enter the live property ID in the box which appears below.

This will mean that when the environment name or the hostname matches, it will provide the relevant Google Analytics property from the lookup table.

Make sure you save this variable using a name that is easy to reference - for example, 'Lookup Environment to GA Property' or 'Lookup Hostname to GA Property'.

Using the variable to load Universal Analytics

Once we have the variable set up, we can now use it to load the correct Universal Analytics code for the environment.

Navigate to the 'Tags' section of your container, and click 'New' to create a new tag.  Choose the predefined 'Universal Analytics' when you click in the middle of the Tag Configuration section, and wait a moment while the tag properties become available for editing.

You can set up Google Analytics variables if you need to reproduce the same settings multiple times, however, for now, check the box to override settings variables.

In the box where the Tracking ID is expected, type two curly braces {{ and then type the name of the Lookup Table variable we created above.  You will notice that typing {{ will list all variables available - these are narrowed down as you start to type.

google tag manager set up universal analytics


Make any setting adjustments that you might need to the Analytics code - for example enabling display advertising features, cross-domain tracking or enhanced e-commerce etc.  Once this is complete, select in the triggering section which pages you require the Analytics to fire on - often this will be all pages - and then save the tag with an appropriate name - for example 'GA - Universal Analytics'.

Deploying your changes

Once you finish adding the analytics tag, make sure you are not loading Analytics through any other tags in your container, or on the website itself.  

Confirm that your container code is published on the relevant pages, and then publish your changes being sure to select the appropriate environment.  

If you are publishing to a development area, open up your Analytics property for the development environment, and head to the 'Real Time' section.  Assuming you have not applied any IP filters, you should be able to see the browsing activity and events when you are in the development area.  Double-check that everything is loading as expected with the Google Tag Assistant - and double check all your other areas are serving up the correct Google Analytics profile.

That's it, you're done! Awesome job!


Need Marketing or SEO support for your project? Work with the experts today!

Virya Group provides a range of solutions to your technology needs