Learn business growth with Google Analytics 4 › Forums › Google Analytics 4 › Disabling Google Tag Manager on localhost to prevent tag firing › Reply To: Disabling Google Tag Manager on localhost to prevent tag firing
-
Yes, there is a way to prevent events firing off to Google Analytics during testing or non-production environments in Google Tag Manager. It involves creating a variable that checks your current environment, and a trigger that fires based on the result of the variable. Here are the steps:
1. Create a **JavaScript variable** within GTM. This variable will check your current domain and return whether you’re in a production or non-production environment. Name it something like “JS – Environment”. In the configuration, you can use a simple **JavaScript snippet** that checks the current URL and compares it to your production URL (something like:
window.location.hostname === 'yourproductionurl.com'
).2. Now, create a **trigger** that fires based on the value of “JS – Environment”. Call this trigger “Production Environment”. Set the trigger type to “Page View” and configure the trigger to fire on some page views where “JS – Environment” equals true.
3. Then, adjust your GA4 Event tags. In each of the tags’ settings, find the triggering section. Add your “Production Environment” trigger to them.
Consequently, this will make GA4 Event tags fire only when your website is running on the production URL, preventing test data from appearing in your Google Analytics reporting.