Using Environments In Google Tag Manager for Your Nonprofit Website

Seth Giammanco By SethG,
Technology


When we setup a CMS driven website we will often include a global field or configuration setting to enter a Google Analytics or Google Tag Manager (GTM) code snippet.

When only the very basic analytics are needed, we'd simply add the code snippet to the live site once it goes live. 

The challenge is when more advanced analytics are needed we like to make sure to test first on a non-public staging server before we go live. It's simply the right thing to do.

So one approach we explored was to create completely different GTM containers and analytics profiles and use them independently in different environments (Staging, Live, ...). 

This works, but as soon as you use GTM to create events or transactions these settings would them need to be made to your staging container and when ready manually re-added to your live container. With each new tag you add to GTM the complexity and inefficiency of this configuration and process grows.

I wanted to find a better way. I wanted a solution with a single GTM container that could be published to staging and live websites in a way that would let us test tags on staging and when confirmed to be working there, efficiently publish them live. Sounds nice, right?

Inspired by a blog on the this topic by Simo Ahava I explored his technique to setup a single GTM container to be used on staging and live instances of our website. The above blog may be a great resource for you as well, but I found some of it confusing, so here is my version.

1. Setup GTM Environments

In GTM, create a new or open your existing container and then click "Admin" in the primary nav. Find the Environments settings link and click it. 

You should notice there are three environments that are always there by default. These include LiveLatest, and Now Editing with the most important for our purposes being Live. We will use this environment for our live or production version of the website.

Next, we are going to add a new environment for Staging. Click the "New" button and complete the form entering Staging as the name, an optional description, and a specific url for the staging environment for your website if you want to use preview.

After you save, I found the version of GTM at time of publishing this article to be confusing. It wants you to publish to this new environment right away. Likely there is no harm in doing so, but I continued until I could close the overlay to skip this step for now. 

If successful, you should be returned to the environment settings page and should see your new custom environment for Staging listed.

2. Installing Environment Snippets

When you create custom environments and want to use them, each environment has a unique GTM install code that must be installed to the appropriate site. You can access the code snippet from the environment settings page.

Click to open the Actions dropdown for the  environment and select “Get Snippet” then copy the code and install it on the appropriate version of the website however you typically would do so.


3. Setup GA Profiles For Each Environment

Though we are only going to need one GTM container, we will still need separate Google Analytics profiles for each version of the site.

You are going to need a profile that you will use for your Live site and another for your Staging site. In Google Analytics, create them as needed and write down their Tracking IDs that look something like this UA–1234567–12.

4. Enable The Environment Variable

Return to GTM and go back to your main container settings page and select "Variables" in the side bar navigation.

Here, under utilities, mark "Environment Name" to enable that built-in variable.


This variable will store the environment name (Staging, Live, …) that a container instance is published to. We’re going to use this in the next step to do something fun.

5. Create A Lookup Table

The goal is that when our container is published to Staging our Google Analytics profile for staging is used. Similarly, when we publish to Live, we want our live profile to be used.

To help make the above happen, we are going to create a custom Lookup Table variable.

From the Variables settings of your GTM container, under “User-Defined Variables” create a new variable and set the type to “Lookup Table”.

Under “Configure Variable” set the input variable to “{{Environment Name}}”. Then create two rows with input of Staging and Live respectively and the output for each being the Google Analytics Tracking ID that goes with that environment.

Don’t forget to change the name of the variable from “Untitled” to something contextual like “GA-ID”.


PLEASE NOTE: Case matters, so type in your environment names exactly as you had done in step #1 above.

6. Setup GA Tags With A Lookup Variable

Now, whenever you have a Google Analytics related tag whether it be for page views or transactions, you should enter the new lookup table variable in place of a specific Tracking ID.


Doing this will automatically set the right analytics Tracking ID for the right environment the container is published to.

7. Putting It All To Work With “Publish To”

Once everything is setup, it is time to put the solution to the test. When you publish your container you will now need to select which Environment you are publishing to.


All your Google Analytics tags setup with the lookup table variable detailed above should automatically have the right Tracking ID based on the environment published to.

You can now create new tags and publish them first to Staging for testing and when ready simply publish the container to Live to deploy your tested changes.

Mission accomplished.

This guide focused on using environments for Google Analytics but certainly this approach could be applied to work with other tracking solutions or unique environment specific data.

If you give this a try, leave a comment or post a tweet to let me know how it goes. Applied this approach to other tracking sources other than Google Analytics that this worked well for? I would love to hear about those as well.