Setup Google Tag Manager Environments using GTM4WP
Google Tag Manager Environments is an advanced feature that allows you to publish different container version into different versions of your website. Environments are useful for organizations that use multi-tiered environments in their development workflow.
You may use a separate “development” version where programmers can easily improve site feature without risking the operation of your live site. It is usually also a good idea to have a “testing” or “sandbox” version where plugin updates can be tested before you upgrade them on your live site. This can prevent you from breaking the site when some new plugin version conflicts with another plugin or the theme itself. This can also allow you to test new improvements and changes in your Google Tag Manager container without the need to publish changes directly on your live site.
Every container row in the plugin settings carries its own environment parameters, so a site that loads several containers can put each of them into a different environment. Both the gtm_auth and the gtm_preview value of a row have to be filled in before that container switches to an environment. Leave both empty to load the live version of it.
This page describes GTM4WP 2.0.0 and newer. Where the legacy 1.x versions of the plugin behaved differently, the older behavior is marked with a bold GTM4WP 1.x: lead-in.
GTM4WP 1.x: the two parameters were entered once on the Advanced tab and applied to the whole site, and only one container could be used with environments. With several container IDs configured, only the first one was loaded.
This page shows you the steps you need to go through in order to setup your GTM environment in GTM4WP.
1. Create your Google Tag Manager environments
First of all, read this GTM help center article to learn how to create your separate environments within GTM.
2. Get the environment parameters
Visit Admin, then Environments under your container and click on the Actions button at the end of the environment, that you are currently setting up.

Select the Get Snippet option from the menu.
3. Find the gtm_auth parameter
Look at the first container code and locate the gtm_auth parameter in it. Copy the value of this parameter to your clipboard (note: the value might continue to show on a new line!)

Open the plugin settings, find the row of your container in the Google Tag Manager container section and paste the value into its Environment gtm_auth column.
4. Find the gtm_preview parameter
Look at the container code again and locate the gtm_preview parameter in it. Copy the value of this parameter to your clipboard.

Paste the value into the Environment gtm_preview column of the same container row.
What the plugin checks when you save
Both values are validated when you save the settings. A gtm_preview value looks like env-4, and a gtm_auth value contains only letters, numbers, hyphens and underscores. A value in any other format is refused with a message that names the container row it belongs to, so a typo cannot end up in the loader URL of your container.
Hard coding for better site migration opportunities
You can also hard code both the container ID and the environment parameters into your wp-config.php file using PHP constants.
