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

  • Henry

    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.