1

We have to setup google analytics to different environments - stage and production for now. Just wondering what is the right convention to follow. I am using google tag manager.

  1. Use tag manager and create multiple environments in the tag manager ? If I use this how do I filter my data from staging and production in the google analytics ?
  2. Filter the staging url views and create a new view only for the staging data ?

Thank you.

1 Answer 1

1

If the production environment is in relation to the live site and has the default GTM container code installed on it, it doesnt need to be specified within GTM.

Every container is set up with a "Live" environment by default, which always points to the container version that is currently published. You don't need to define the Live environment.

Other environments will each have their own specific container code, installed to the applicable environment eg the container code for the staging environment will be installed on the staging site website.

Install your container snippets. This process is similar to installing a standard Tag Manager container snippet, except that you have to do this on every server that has a corresponding environment defined in Tag Manager. This step may require assistance from a developer or systems administrator to complete.

  1. In the Custom Environments listing, find the entry for the environment you would like to install.
  2. Click the Actions menu for that entry.
  3. Select Get Snippet.
  4. Copy the resulting code snippet and install it so that it will appear in the code of the target environment.
  5. Save and publish the website's code changes.

When you have custom environments defined, the Publish dialog will add a menu so that you can define to which server the changes will be published.

  1. Click Publish in the upper right corner of Tag Manager.
  2. Choose a selection from the Environment menu for where you want to publish your changes.
  3. Click Publish.

More info on using Environments can be found in the GTM Help Center https://support.google.com/tagmanager/answer/6311518?hl=en

There are so many ways to go about setting this up.
It really depends on your preferences and situation.

You can send hits to different GA Properties based on the Environment name, or you can send hits to different GA Properties based on a Hostname, you can also send hits to different GA Properties based on Hostname & Debug Mode (and the list goes on).

To send hits to different GA Properties based on the Hostname
1. Ensure the inbuilt variable for Page Hostname is turned "on" (it should be by default).
2. Create a Lookup Table variable, name it along the lines of Lookup - GA Tracking IDs.
3. For Input Variable, use {{Page Hostname}}.
4. In the first table row, for Input enter the hostname for your live site, and for Output, enter the live site's GA Tracking ID.
5. In the second table row, for Input enter the hostname for your staging site, and for the Output, enter the staging site's GA Tracking ID.
6. Plug {{Lookup - GA Tracking IDs}} variable into Tracking ID option of the GA Settings Variable.
enter image description here

To send hits to different GA Properties based on Environment
1. Turn "on" the builtin variable for Environment Name.
2. Create a Lookup Table variable, name it along the lines of Lookup - GA Tracking IDs.
3. For Input Variable, use {{Environment Name}}.
4. For Input value, enter the Environment Name, for Output value, enter the GA Tracking ID for the site that has the relevant Environment's container snippet installed.
5. Set the Default Value to the GA Tracking ID for the Property for the live website.
6. Plug {{Lookup - GA Tracking IDs}} variable into Tracking ID option of the GA Settings Variable.
enter image description here

4
  • Thanks. How do I setup my google analytics for my staging environment, when both my production and live environments are using the same tags ? Should I create a view just for this new domain or should I have a new property ?
    – hck3r
    Commented May 24, 2020 at 21:39
  • If you use a different property just for debugging purposes, when you setup a lookup table as per the above explanation/screenshots, when in GTM debug and preview mode, only hits sent while in GTM debug/preveiw mode will get sent to the debug property, the hits wont get sent to the Property that is tracking the live site.
    – Bronwyn V
    Commented May 24, 2020 at 22:50
  • So if I understand it right, its one single tag but has different tracking ids ? eg: live.com has a tag but a tracking ID 'A' and I will create a new property for staging.com which will have a tracking ID 'B'. and all the hits to staging.com will show up in the staging.com view, with the above mentioned setup ?
    – hck3r
    Commented May 25, 2020 at 10:21
  • Actually the example i gave wasn't particularly helpful. Have edited my answer for better clarity and for your specific situation. But yes, single tag using different tracking ids, however you can set up the GA settings to send data to a specific GA Property based on either hostname or a particular GTM environment.
    – Bronwyn V
    Commented May 25, 2020 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.