{"id":951,"date":"2022-05-16T17:45:26","date_gmt":"2022-05-16T16:45:26","guid":{"rendered":"https:\/\/organicdigital.co\/blog\/?p=951"},"modified":"2024-06-10T15:12:00","modified_gmt":"2024-06-10T14:12:00","slug":"how-to-set-up-google-analytics-and-ga4-on-multiple-environments","status":"publish","type":"post","link":"https:\/\/daveashworth.co\/blog\/how-to-set-up-google-analytics-and-ga4-on-multiple-environments\/","title":{"rendered":"How To: \u00a0Set Up Google Analytics and GA4 on Multiple Environments"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>As a <a href=\"https:\/\/daveashworth.co\/skills\/analytics-and-ad-tracking-consultant\/google-analytics-4\/\">Google Analytics consultant<\/a> I get asked many questions about GA, such as:<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/daveashworth.co\/blog\/how-to-set-up-google-analytics-and-ga4-on-multiple-environments\/#What_Are_Google_Analytics_Environments\" >What Are Google Analytics Environments?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/daveashworth.co\/blog\/how-to-set-up-google-analytics-and-ga4-on-multiple-environments\/#Why_Set_Up_Multiple_Analytics_Views\" >Why Set Up Multiple Analytics Views<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/daveashworth.co\/blog\/how-to-set-up-google-analytics-and-ga4-on-multiple-environments\/#How_To_Set_Up_Views_on_Universal_Analytics\" >How To: Set Up Views on Universal Analytics<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/daveashworth.co\/blog\/how-to-set-up-google-analytics-and-ga4-on-multiple-environments\/#Google_Analytics_4_Uses_Data_Streams_Not_Views\" >Google Analytics 4 Uses Data Streams, Not Views<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/daveashworth.co\/blog\/how-to-set-up-google-analytics-and-ga4-on-multiple-environments\/#How_To_Set_Up_Lookup_Tables_in_Google_Tag_Manager\" >How To: Set Up Lookup Tables in Google Tag Manager<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/daveashworth.co\/blog\/how-to-set-up-google-analytics-and-ga4-on-multiple-environments\/#How_To_Set_Up_RegEx_Variables_in_Google_Tag_Manager\" >How To: Set Up RegEx Variables in Google Tag Manager<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Are_Google_Analytics_Environments\"><\/span>What Are Google Analytics Environments?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Set_Up_Multiple_Analytics_Views\"><\/span>Why Set Up Multiple Analytics Views<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.&nbsp; Or worse still, there\u2019s 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).<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>The following methods will help you to do this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_To_Set_Up_Views_on_Universal_Analytics\"><\/span>How To: Set Up Views on Universal Analytics<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Within your GA property and accounts, you can set up a view which is essentially sub-account that has it\u2019s only configuration in terms of filters.&nbsp; <\/p>\n\n\n\n<p>Common use of filters include showing traffic in a certain region, or on various devices \u2013 e.g. desktop, mobile or tablet.<\/p>\n\n\n\n<p>You can easily include or exclude visitors via any of the standard GA dimensions.<\/p>\n\n\n\n<p>In this case, we would use \u201chostname\u201d, and then configure accordingly.<\/p>\n\n\n\n<p>To do this, from your main GA account, click \u201c<strong>create view\u201d<\/strong> within the relevant property:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"896\" height=\"294\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/create-view.jpg\" alt=\"Create View\" class=\"wp-image-953\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/create-view.jpg 896w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/create-view-300x98.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/create-view-150x49.jpg 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/create-view-768x252.jpg 768w\" sizes=\"(max-width: 896px) 100vw, 896px\" \/><\/figure>\n\n\n\n<p>Let\u2019s 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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"714\" height=\"488\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/new-reporting-view.jpg\" alt=\"Reporting View Name\" class=\"wp-image-954\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/new-reporting-view.jpg 714w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/new-reporting-view-300x205.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/new-reporting-view-150x103.jpg 150w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/figure>\n\n\n\n<p>Once done, click \u201c<strong>create view\u201d<\/strong>, then select \u201c<strong>Filters\u201d <\/strong>in your new view:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"402\" height=\"303\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/filters.jpg\" alt=\"Filters\" class=\"wp-image-955\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/filters.jpg 402w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/filters-300x226.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/filters-150x113.jpg 150w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>Here, you can create a filter to only show data from your test site by using a <strong>\u201cpredefined\u201d<\/strong> filter type that \u201c<strong>includes\u201d -&gt; \u201ctraffic to the hostname\u201d -&gt; \u201cthat are equal to\u201d<\/strong> then enter your test site hostname \u2013 in this example it\u2019d be <em>test.daveashworth.co<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"641\" height=\"546\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/add-filter.jpg\" alt=\"Add Filter\" class=\"wp-image-956\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/add-filter.jpg 641w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/add-filter-300x256.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/add-filter-150x128.jpg 150w\" sizes=\"(max-width: 641px) 100vw, 641px\" \/><\/figure>\n\n\n\n<p>Click <strong>\u201csave\u201d <\/strong>and that\u2019s it \u2013 you now have a view that will only collect data from your test environment.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>For custom dimensions, click the <strong>\u201ccustom\u201d <\/strong>option \u2013 if you then wanted to only include say UK traffic, you\u2019d select either include or exclude, the filter field set to \u201cCountry\u201d and the filter pattern to the name of the country:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"655\" height=\"711\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/location-filter.jpg\" alt=\"Filter Name\" class=\"wp-image-957\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/location-filter.jpg 655w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/location-filter-276x300.jpg 276w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/location-filter-138x150.jpg 138w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/figure>\n\n\n\n<p>The benefit of doing this is that you can leave one account ID in place tracking on all environments and don\u2019t need to worry about updating analytics settings when a site goes live, or sync\u2019d back to dev etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Google_Analytics_4_Uses_Data_Streams_Not_Views\"><\/span>Google Analytics 4 Uses Data Streams, Not Views<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The thing is, GA4 doesn\u2019t have views, and (as far as I can see) it\u2019s not possible to set up filters on hostnames.&nbsp; To be fair, there is a lot to&nbsp; learn in GA4 ahead of it\u2019s 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.<\/p>\n\n\n\n<p>In the mean time, you can use <a href=\"https:\/\/daveashworth.co\/skills\/google-tag-manager-consultant\/\">Google Tag Manager<\/a> to send data to different accounts based on hostnames:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_To_Set_Up_Lookup_Tables_in_Google_Tag_Manager\"><\/span>How To: Set Up Lookup Tables in Google Tag Manager<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>I love Lookup Tables as they have many different uses for manipulating data outputs based on various inputs.<\/p>\n\n\n\n<p>In this case, let\u2019s say we\u2019ve decided to set up two separate analytics properties for GA4 \u2013 one for the live site, one for the staging environment.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"900\" height=\"444\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/lookup-table.jpg\" alt=\"Lookup Table\" class=\"wp-image-958\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/lookup-table.jpg 900w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/lookup-table-300x148.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/lookup-table-150x74.jpg 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/lookup-table-768x379.jpg 768w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p>This takes the \u201cpage hostname\u201d as the variable, then you set the GA account ID depending on the environment<\/p>\n\n\n\n<p>Then, in you set up one GA4 page view\/config tag for all pages, which passes the variable:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"887\" height=\"624\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/ga4-config.jpg\" alt=\"GA4 Config\" class=\"wp-image-959\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/ga4-config.jpg 887w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/ga4-config-300x211.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/ga4-config-150x106.jpg 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/05\/ga4-config-768x540.jpg 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_To_Set_Up_RegEx_Variables_in_Google_Tag_Manager\"><\/span>How To: Set Up RegEx Variables in Google Tag Manager<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.vakulski-group.com\/blog\/post\/how-to-use-a-hostname-filter-in-ga4\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.vakulski-group.com\/blog\/post\/how-to-use-a-hostname-filter-in-ga4\/<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Setting your configuration up in GTM means you can use the same code on both your live and test environments, and don\u2019t have to worry about having different code within each environment.<\/p>\n\n\n\n<p>As soon as I get my head around data streams, I\u2019ll do a post about them.<\/p>\n\n\n\n<p>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\u2019t hesitate to get in touch, am always happy to help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find out various ways to ensure your test\/staging data doesn\u2019t dilute your live site data using Google Analytics views and Google Tag Manager.<\/p>\n","protected":false},"author":1,"featured_media":965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analytics"],"_links":{"self":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":0,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media\/965"}],"wp:attachment":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/tags?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}