Managing Site Environments with Google Analytics and Google Tag Manager

by Peter Adamson

Updated by Rob English, Analytics Implementation Specialist (June 26, 2019)

Since this post was originally published, our internal process and recommendation on how to set the Tracking ID and other GA tag parameters has changed.  We now recommend using the Google Analytics Settings Variable.

As originally documented, in order to set the Tracking ID for our Google Analytics tags, we mapped the {{lookup – GA property from environment variable}} directly to the Tracking ID field on each of our GA tags.  This can be accomplished through the Google Analytics Settings Variable.

In Google Tag Manager, create a new variable, and from the ‘Choose variable type’ menu, scroll down to Utilities and select the Google Analytics Settings variable. Name your variable, and map {{lookup – GA property from environment}} to the Tracking ID field.  Save your changes.


Next, go in to your Google Analytics tag.  Map your newly created GA settings variable to the Google Analytics Settings field.  Save your changes.

The benefits of using the Google Analytics Settings Variable can be seen when you start getting into containers where you have multiple GA tags.  Whereas before you would need to manage settings like Fields to Set, Custom Dimensions, etc. on a tag by tag basis, you can now define these once in your settings variable and map to each of your GA tags, simplifying the addition, removal and updates made on these settings.

Original published by Peter Adamson, Analytics Implementation Specialist 

I want to discuss an underused feature in Google Tag Manager (GTM) that Napkyn Analytics recommends for all of our clients.

The GTM environments feature allows different versions of the same GTM container to be selectively published to different environments. Most large websites will have an established release process that includes multiple environment tiers: updates transition from development, to testing, to release. Even many small sites will have at least a non-production environment for testing before making updates. GTM can support this process by allowing new container versions to be published to lower-tier environments before going to production.

This post steps through setting up GTM environments and leveraging the GTM environments feature to configure Google Analytics (GA) tracking based on the environment, keeping production data clean of testing data.

Configure Tag Manager Environments

To get started, go to the ‘Admin’ tab in the GTM container and under the ‘Container’ column you’ll see ‘Environments’.

Ideally you should create a new GTM environment for each non-production environment in your site release process, and give them names consistent with those. You don’t need to create a custom environment for your production site; the built-in ‘Live’ environment will be used for production.

Deploy the Tag Manager Environment Snippets

GTM environments have different snippets that will need to be deployed to the appropriate environments.

In the ‘Environments’ menu, select ‘Get Snippet’ from the ‘Actions’ menu of each custom environment created.

Remember to use the snippet for the ‘Live’ environment from within the ‘Environments’ menu. It is different from the default snippet you get from the ‘Install Google Tag Manager’ link in the Container column of the admin.

You might notice that the only difference between these environment snippets is in the query string. The custom environments have different values for ‘gtm_auth ’ and ‘gtm_preview ’, while the default snippet won’t have these parameters at all.

The deployment of environment snippets is going to be the same in all cases. Follow the instructions you get in GTM. The JavaScript snippet should be placed on your pages as early in the HTML as possible, optimally just after the opening <head>  tag and <meta charset>  tag. The <noscript>  version should be placed immediately after the opening <body>  tag.

Different Google Analytics Properties for Different Environments

To make the most of GTM environments, you should have separate Google Analytics properties for each environment. By that I mean development, staging, production, and other environments you use should each have their own Google Analytics property ID (the ‘UA-XXXXXX-X ’ part). At the very least you should have at least two properties in GA: one for production data and one for everything else. This is meant to keep your production GA property free of test data that will pollute the ‘real’ data.

Set Up Your GTM Variables

In GTM, for each GA property ID you have for your site, create a ‘constant’ type variable and save it. This is useful to provide a name for these otherwise similar values within GTM, makes them available to other GTM elements, and creates a single place to update them in the future.

Next, ensure you’ve enabled the built-in ‘Environment Name’ variable.

Now create a new ‘Lookup Table’ variable. The input variable will be the built-in ‘Environment Name’ variable. Assign property variables to their appropriate GTM environments for the output. Remember the input value here is case-sensitive, the built-in ‘Live’ environment is in title case, and your custom environments are exactly the names you’ve given them.

Add a row for your production GA property, set the default to the lowest tier GA property, and add more rows for every other environment/property you wish to use. Save the lookup table and move on to configuring your Google Universal Analytics tag.

Configure Your GA Tags

You will want to configure all of your Google Analytics tags to use the variable you’ve just created.

In the ‘Tracking ID’ field, click the brick button on the right, and choose the lookup table variable you’ve just created. This will populate the field with that GTM variable, and wrapping the variable name with the double curly brackets GTM uses to denote variables.

Testing the Setup

So, one awkward thing about using built-in ‘Environment Name’ variable in GTM is that it is difficult to test without publishing. One of the useful features of GTM’s Preview is that it ignores the environment, and allows workspaces and versions to be tested anywhere the same container ID is deployed. The ‘Environment Name’ in preview mode will be different from any environments configured in the container.

When previewing a GTM workspace the value will be ‘Draft Environment – something’. When previewing previously-saved container versions, the environment name in preview mode will be an empty string.

To test the other environment/GA configurations, though, you will need to eventually publish to verify that the GA property ID is correct. If you have other environments between the default/lowest and production, you can take advantage of the main feature of GTM environments and selectively publish the configurations through the lower environments first, before production.

As you publish through the environments, Google Tag Assistant, or similar tools can be used to see what tracking ID data is being sent to.

That’s a fairly quick walkthrough of GTM environments, but I hope you found it useful! If you have questions, be sure to reach out to us.

Peter Adamson

Implementation Specialist, and Google Tag Manager Practice Lead

Peter is an Implementation Specialist and Practice Lead for Google Tag Manager. He partners with our enterprise client development stakeholders to help guide them through and assure the quality of their most sophisticated analytics infrastructure requirements.

See more posts from Peter