If you have started using the “environments” feature in Google Tag Manager (if not, read why you should at Simo Ahava’s blogpost on environments), then maybe you’ve experienced some weird/confusing behaviour when using the preview/debug functions.
You may have also noticed the gtm_cookies_win=x in your tracking snippet.
After searching for information about the gtm_cookies_win parameter, and not finding anything, I decided to try and debug my way out of it.
Basically, what I came up with is this:
gtm_cookies_win=x allows GTM Preview to change the environment of a page
The clue is sort of in the name: “GTM cookies win”. Meaning they win over your gtm_auth and gtm_preview parameters.
If you install the above Staging snippet on you staging site, and you then go to GTM and hit “Preview” on your container, then you would notice that your “Environment Name” variable would be empty when you visit you staging server.
This is a bit weird, since we are on the staging server, right?
If you instead go to the GTM staging environment and use the “Share Preview” link, and then visit the staging site, then the “Environment Name” would be “Staging”. But now visit the Live site, and the environment would still be “Staging”. What’s up with that?
Well, it turns out, that by setting gtm_cookies_win=x, you effectively let the GTM editor decide which environment they want to use, regardless of what you put in you tracking snippet. I think that is pretty confusing, and I’m guessing editors feel the same way.
So what we’ve done instead is to remove the gtm_cookies_win=x from the tracking snippet. This effectively means that previewing tags will no longer work. But since you have multiple environments, you don’t have to preview any more.
Your workflow will now look like this instead:
- Create you new GTM tags
- Publish the container to your GTM “Staging” environment
- Hit “Preview” and go to your staging website, and check that the tag works as expected
- Publish the container to you GTM “Live” environment
There. Now you know that whenever you visit the staging website, then the “Environment Name” will be “Staging”, regardless of whether you are in preview mode or not.