Learn business growth with Google Analytics 4 Forums Google Analytics 4 Troubleshooting issue with Google Analytics 4's set method

  • Troubleshooting issue with Google Analytics 4's set method

    Posted by Alexander on 5 July 2022 at 1:18 am

    Hey there, I’ve got a bit of a head-scratcher here. I’m working on setting up Google Analytics 4, not using Google Tag Manager just to be clear. According to the Google docs, these two bits of code should do the same thing:

    First off, we’ve got this:

    `javascript
    gtag(‘event’, ‘testing’, {‘foo’:’x’});
    `
    And then there’s this:

    `javascript
    gtag(‘set’, {‘foo’:’x’});
    gtag(‘event’, ‘testing’);
    `
    Now here’s the kicker: the first one is working just as I’d hoped, but the second one? Not so much. It’s like the ‘set’ bit just goes in one ear and out the other; the property ‘foo’ is ignored. What gives? Any ideas what could be causing this peculiar behavior?

    Henry replied 1 year, 2 months ago 3 Members · 2 Replies
  • 2 Replies
  • Ava

    Member
    2 June 2023 at 4:57 pm

    The issue you’re encountering arises from how Google Analytics 4 handles parameters. When you use “set”, it configures certain default fields to be sent with all tracking hits. However, this doesn’t apply to event parameters as Google Analytics 4 may not persist these parameters across all events.

    In your first code block, you’re sending ‘foo’ as an event parameter which is correct and this is why it is working as intended.

    In your second code block, when you use “set” you’re trying to send a default field, but this won’t apply to an event unless explicitly defined. ‘foo’ is ignored because it’s not a predefined field that is recognized by GA4. If you wanted to set a global value to be sent with every event, you could use ‘user_properties’ in GA4 set-up. Essentially, the difference between the examples you shared is based on the scope of the parameters you’re trying to set/send with your events. This is why they’re not yielding the same result.

  • Henry

    Member
    20 June 2023 at 2:30 am

    The difference between the two snippets of code comes down to when and where the ‘foo’ value is set. In the first snippet, gtag('event', 'testing', {'foo':'x'});, the ‘foo’ variable is set directly within the event context. This means ‘foo’ will be sent with this event only.

    For the second snippet, gtag('set', {'foo':'x'}); gtag('event', 'testing');, ‘foo’ is set as a global configuration parameter, which should be applied to all subsequent events. But there’s a nuance – it does not append that parameter to the events that have already been sent or are queued to be sent. Possibly your ‘testing’ event has already been put into the queue by the time the ‘set’ command is executed.

    Also, it’s important to bear in mind that the ‘set’ command in GA4 doesn’t work like it did in Universal Analytics. In GA4, setting a parameter occurs on a per-event basis. If you want ‘global’ parameters, you have to use event defaults or user properties. If you want to associate ‘foo’ with every ‘testing’ event, you may need to modify your data layer or send ‘foo’ with every ‘testing’ event.

Log in to reply.