It would be great to have different Google Analytics tracking ID for different domains or environments like the production, qa and test websites. Wouldn’t it?

Having the same tracking ID on all your sites is really not a good practice.

On top of this, it would be awesome if you could manage everything from one single tag with Google Tag Manager.

Indeed, one single tag in GTM, with some kind of logic. The tag will know what Google Analytics property to use on which domain, website or environment.

If you have the following setup: One Google Analytics account with two properties, (one for your production website and one for you staging website), and you are wondering how to implement this in a smart way with Google Tag Manager, you are on the right page.

After reading this guide, you will know how to guarantee that each environment in your QA workflow (development, pre-production and production) has its own Google Analytics tracking ID (UA-XXXXXX-X)

Editor’s note: If you’re not yet comfortable with the configuration of environments in Google Tag Manager, I advise you to read the previous tutorial.

In the following tutorial we will cover these different topics:

  • How to create a new property in Google Analytics
  • How to enable the Environment Name variable in the Google Tag Manager
  • How to create on Constant per Google Analytics property in Google Tag Manager
  • How to create a Lookup Table in the Google Tag Manager
  • How to create one tag in Google Tag Manager with several Google Analytics properties

Sounds complicated? If you follow the next steps, everything should go well.

Creating a new Google Analytics property for your test website

From Google Analytics, we will create a new property that can be used in the test or pre-production environment:

  • Go to the Admin panel of Google Analytics.
  • At the top of the middle column click on Create Property
  • Give the property a name such as “Your website (STAGING)” and click on Get Tracking ID

You will have two Google Analytics Tracking ID (UA-XXXX-1 and UA-XXXX-2).

 Google Analytics Tracking ID

The rest of the setup continues in Google Tag Manager.

Enable the “Environment Name” variable in Google Tag Manager

The next tag require a new variable, “Environment Name“. In Google Tag Manager, do the following:

  • On the Workspace panel go to Variables
  • Click on Configure
  • Tick the box Environment Name
Enable the Environment Name variable in Google Tag Manager

Store the Google Analytics Tracking ID in a Constant

The next step is to create a new Constant to store the Staging website Tracking ID.

  • On the Workspace panel go to Variables
  • Click on New under the section User-Defined Variables
  • Select the type Constant
  • In the Value field enter the Tracking ID
One Google Analytics Variable per Environment

You should have now two variables in Google Tag Manager: One for Production and one for Staging.

Two variables in Google Tag Manager: One for Production and one for Staging.

Please note that the type of the variable is Constant and not Google Analytics Settings. The Constant is needed for the next step when we will set up the Lookup Table.

Create a Lookup Table in Google Tag Manager

A Lookup Table it a simple matrix that will output a value based on the input.

In our case, based on which environment the Google Tag Manager container is deployed, the table will return the corresponding value of the Google Analytics Tracking ID.

The setup is easier than the definition.

From the Workspace panel of Google Tag Manager do the following:

  • Click on the Variables menu.
  • Create a new User-Defined Variable
  • Select Lookup Table as a Variable
  • As an Input Variable select the Environment Name
  • Add one row per environments
  • For each row, set the Input value with the exact name of your environment, and as an output the corresponding Google Analytics Tracking ID variable

The setup of the Lookup Table should look like this.

Configuration of a Lookup Table for multiple Google Analytics Property with GTM

The Lookup Table will return a specific Tracking ID based on the environment.

And now the last and final step.

Update the Universal Analytics Tag with the Lookup Table

Finally, we have to review and update the configuration of the Universal Analytics Tag, and include the Lookup Table.

Follow this step in Google Tag Manager:

  • Go back to the Tag Panel
  • Select the Google Analytics Tag previously created
  • Tick the box Enable overriding settings in this tag
  • Select for Tracking ID the Lookup Table you just created
Update the Universal Analytics Tag with the Lookup Table

Finally, you still have one single Google Analytics Tag. But this one embeds some logic. Based on the environment, it will output the right Google Tracking ID.

To see your change, you will have to publish all the recent modification. And of course, publish first on Staging. Test it, and then push it to Live.

Go further with Google Tag Manager and Google Analytics

Congratulations, you have implemented Google Tag Manager and Google Analytics in a multi-tenant architecture. One instance of Google Tag Manager with Google Analytics serves multiple servers and websites.

This article is part of on advanced tutorial of Google Tag Manager and Analytics.

I invite you to continue configuring your site by setting up events and goals with the help of Google Tag Manager.

Join the conversation