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.
Table of Contents
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).
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
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
You should have now 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.
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
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.
So this seems to work when I use the environement-specific link for preview. But it doesn’t seem to work otherwise. I’ve published the version to Live, but my GA tag is not registering anything in realtime. It only does when I use the environment-specific link for preview. Any thoughts?
Thank you so much for this! Worked perfectly and saved me loads of time.
Hi, i was just wondering what do you mean when you say “publish first on Staging”? How can i publish on only one Enviroment?
maybe you should check this post as well, Im detailing a bit more the “environement” feature.
My point is that you can publish your change to a test website first, before pushing to the live one.
Samuel, great guide. Thank you. Quick question.
Could you elaborate on this:
“Having the same tracking ID on all your sites is really not a good practice.”
I have one property for the root domain, and then different views within that property for each of the different subdomains (www., app., and staging.). Is this problematic?
Samuel, Great Guide.
I used to have different copies of the same settings for each environment..
This will definitely save me a lot of work, efforts.. and errors !!
When i create a new property in GA for my sub domain (used for testing) it does not give me a UA- number just a 2xxxxxx number. How do i get a UA tag for my sub domains?
Hi! Thanks for the guide.
I’m trying something similar and wonder what’s the purpose to store the Google Analytics Tracking ID in a Constant? Will it not work if I just add each tracking ID as output in the lookup table?
you could try without constant, but I think it’s a cleaner way, also you can use constant elsewhere and update the constant data once to change it in your entire configuration.
True, sounds like a cleaner way. Thanks!