Learn business growth with Google Analytics 4 Forums Google Analytics 4 Disabling Google Tag Manager on localhost to prevent tag firing

  • Disabling Google Tag Manager on localhost to prevent tag firing

    Posted by Sawyer on 23 May 2022 at 3:47 am

    Hey there! Need some help with Google Tag Manager that you’ve just installed on your website? Exciting, isn’t it? Now, I know you’ve begun firing some events from GTM and you’re able to see them come up when using the tag assistant debugger. And that’s great! You’ve got tags set up which send events to Google Analytics too.

    snapshot

    Now, here’s what you’re struggling with – you want to easily configure all your tags of type GA4 Event to not fire when the trigger happens in a non-production environment. Essentially, you want to test things out, but don’t want these events to take off to Google Analytics in non-production environments. How do you do this with Google Tag Manager, you wonder?

    You’ve even noticed that when running the tag assistant debugger there’s a field called debug_mode which is set to true. You’ve tried giving it a go in your local environment like this:

    export function maybeSetDebugMode() {
      if (typeof window !== 'undefined' && process.env.NODE_ENV !== 'production') {
        window?.dataLayer?.push({"debug_mode": "true"})
      }
    }
    

    But no luck, huh? Your custom events continue to rush off to GA:

    snapshot

    So, you’re wondering, is there a way to tell Tag Manager “Hey, hold up! Don’t pipe events to GA” when a certain condition is met, like matching a URL or the value of some other event field? Or you’d like to know if Google Analytics can say “Thanks, but no thanks!” to events during ingestion which meet some criteria?

    Mia replied 10 months, 4 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • Quinn

    Member
    13 February 2023 at 4:21 am

    Of course, there are practical solutions to avoid firing events to your GA from non-production environments – or when debugging. Indeed, you can also stop GA from recording those events. Let’s go through the process.

    Generally, to start firing events to GA4 from GTM, you need a GA4 configuration tag, which requires a measurement ID. But hey, no one said you have to put just the measurement ID, right? You can set it to a CJS:

    Sample screenshot

    Just refer to it in your config tag, like this:

    Sample screenshot

    This way, whenever you’re working in your lower environment, your local host, or previewing the container on production, your tags and triggers will still fire, but these events will go to a different GA4 property.

    And before you ask, it’s actually a good habit to collect your events from lower environments, too – just don’t mix them with your production data. There may come times when you’ll need to review some test data.

    Moving on to the second part, GA4 does have data filters. While they’re not as robust as the old filtering system in GA UA, you’ll find the developer traffic filter useful. You’ll just need to add a custom parameter to every developer event that you want visible in the debug view, but not in the reports.

    Given GA4’s current limitations and susceptibility to occasional hiccups, it’s generally better to resolve this on the GTM end and reserve GA’s filters for situations that GTM can’t handle.

    Remember that GTM has built-in variables you can use, although most of them are turned off by default. When you type {{ and start typing the variable name in your CJS, this will come up:

    Sample screenshot

    Click on the option you want, and it’ll autofill for you. But do remember to interpret it as whatever the variable returns. They’re mostly strings, but you can also return a function and execute it like {{cjs that returns a function}}(param1, param2). Here’s an example:

    Sample screenshot

    Easy, right? The only thing is, debugging these built-in variables in the console can be a little tricky, so I usually just fetch the hostname from the global object, like this: window.location.hostname.

  • Mia

    Member
    1 March 2023 at 2:42 am

    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.

Log in to reply.