Analytics

How To:  Set Up Google Analytics and GA4 on Multiple Environments

By on 16th May 2022

Reading Time: 4 minutes

  • LinkedIn
  • Twitter
Multiple Envrionments

As a Google Analytics consultant I get asked many questions about GA, such as:

What Are Google Analytics Environments?

Google Analytics environments in platforms designed to gather and analyze data from websites and mobile applications. They allow users to track various metrics, such as website traffic, user engagement, and conversion rates. By implementing Google Analytics on their platforms, businesses can gain valuable insights into user behavior and make data-driven decisions to optimize their online presence.

Why Set Up Multiple Analytics Views

Some of the most common analytics issues I come across are when a new website is being launched and either the data is diluted with page views and transaction data from the test environment, or that a test account is used in a development environment, that is left in place on launch.  Or worse still, there’s no analytics account on the test environment and the site goes live with no analytics tracking in place (which is more often than not only noticed a few days later).

However, there is no reason that you cannot use one analytics account or tag manager container on both your live and test environments to ensure no data is diluted, or lost when the site is updated.

The following methods will help you to do this.

How To: Set Up Views on Universal Analytics

Within your GA property and accounts, you can set up a view which is essentially sub-account that has it’s only configuration in terms of filters. 

Common use of filters include showing traffic in a certain region, or on various devices – e.g. desktop, mobile or tablet.

You can easily include or exclude visitors via any of the standard GA dimensions.

In this case, we would use “hostname”, and then configure accordingly.

To do this, from your main GA account, click “create view” within the relevant property:

Create View

Let’s assume you already have a live website property already collecting data, so we are setting one up for your test environment, so give it a relevant name:

Reporting View Name

Once done, click “create view”, then select “Filters” in your new view:

Filters

Here, you can create a filter to only show data from your test site by using a “predefined” filter type that “includes” -> “traffic to the hostname” -> “that are equal to” then enter your test site hostname – in this example it’d be test.organicdigital.co

Add Filter

Click “save” and that’s it – you now have a view that will only collect data from your test environment.

If you wanted to create a view (or amend your existing view) to then only show live site data, you would set this as include data to that hostname, or exclude from your dev site and so on.

For custom dimensions, click the “custom” option – if you then wanted to only include say UK traffic, you’d select either include or exclude, the filter field set to “Country” and the filter pattern to the name of the country:

Filter Name

The benefit of doing this is that you can leave one account ID in place tracking on all environments and don’t need to worry about updating analytics settings when a site goes live, or sync’d back to dev etc.

Google Analytics 4 Uses Data Streams, Not Views

The thing is, GA4 doesn’t have views, and (as far as I can see) it’s not possible to set up filters on hostnames.  To be fair, there is a lot to  learn in GA4 ahead of it’s take over of analytics in 2023, and as soon as I suss out how to do hostname filters in one account, I will blog about it.

In the mean time, you can use Google Tag Manager to send data to different accounts based on hostnames:

How To: Set Up Lookup Tables in Google Tag Manager

I love Lookup Tables as they have many different uses for manipulating data outputs based on various inputs.

In this case, let’s say we’ve decided to set up two separate analytics properties for GA4 – one for the live site, one for the staging environment.

We can then set up a look up table to take the hostname, and output the relevant account ID using a custom variable as follows:

Lookup Table

This takes the “page hostname” as the variable, then you set the GA account ID depending on the environment

Then, in you set up one GA4 page view/config tag for all pages, which passes the variable:

GA4 Config

How To: Set Up RegEx Variables in Google Tag Manager

I came across this article which allows you to set up hostname variables using a RegEx variable if you so wish, as well as creating hostname filters:

https://www.vakulski-group.com/blog/post/how-to-use-a-hostname-filter-in-ga4/

Setting your configuration up in GTM means you can use the same code on both your live and test environments, and don’t have to worry about having different code within each environment.

As soon as I get my head around data streams, I’ll do a post about them.

In the meantime, if you have any questions on anything withing this post or need help getting your analytics and tag manager config up and running, please don’t hesitate to get in touch, am always happy to help.

Get In Touch

Fill in the form below if you want to enhance your website's organic visibility