Google Analytics 4 (gtag js) 'set' command not adding data to events
Asked Answered
B

1

8

I'm trying to add data to each event I send in GA4 via javascript by using the 'set' command:

https://developers.google.com/tag-platform/gtagjs/reference#set

From those docs, it appears to be similar to Serilog Enrichment, but it doesn't appear to work and I don't see this data coming through.

I'm using localhost + Google Analytics Debugger chrome extension. Then in the Analytics > Configure > DebugView I see the custom event 'hello-world' and the property 'test', but I don't see the data I add via the "set" command.

GA DebugView

I use the set command for 2 calls - first is the "user_id" property that does work. That must be a special case, since GA treats that differently. The 2nd is for the custom object that doesn't work.

Console shows some output for both set command calls, but nothing to tell me that something has succeeded or failed

<!DOCTYPE html>
<html>

<head>
  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=G-SOMECODE"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag() {
      window.dataLayer.push(arguments);
    }
    gtag('js', new Date());

    // Set enrichment properties for every event "on this page"
    // https://developers.google.com/tag-platform/gtagjs/reference#set

    gtag('set', {
      'foo': 'bar',
    });

    // Set the measurement id
    // https://developers.google.com/analytics/devguides/collection/gtagjs/setting-values
    gtag('config', 'G-SOMECODE');

    //Set the GA4 user_id and keep it set for all events
    gtag('set', {
      'user_id': 'a24b935c-03cd-47f0-af68-c60a68b31303'
    });

    gtag('event', 'hello-world', {
      'test': true
    });

  </script>
  <title>Html Delivery</title>
  <script type="text/javascript">
    function myFunction() {
      // Event with nested data test. It doesnt seem to display nicely
      gtag('event', 'button-click-nested', {
        'data': {
          'type': 'nextButton'
        }
      });
      gtag('event', 'button-click', {
        'type': 'nextButton'
      });
    }

    function LinkFunction() {
      console.log("link click");
      gtag('event', 'link click', {
        'type': 'link'
      });
    }
  </script>
</head>

<body>
  <button onclick="myFunction()">Next</button>
  <a id="myLink" href="#" title="Click to do something" onclick="LinkFunction()">link text</a>
</body>

</html>

I've tried to move the calls to above/below the config command, but it makes no difference either.

There is a similar question, but my rep wont let me comment on it to see if its still the case. The accepted answer doesn't really help me since I wanted to be able to add any arbitrary data in this way.

I have tried to setup a custom metric for this in GA (im not using GTM), but still. No data comes through.

Does this just not work?

Bindweed answered 24/3, 2022 at 16:7 Comment(5)
Hey have you found any solution for this? I'm also having trouble with the same issue you had.Monopolist
@NenadVracar, sorry but no I didn't. I ended up adding the shared properties to each event via the regular 'event' command. Making sure we store those properties somewhere globally available.Bindweed
I did exactly the same thing and it looks like the set method is broken.Monopolist
Do you want to "send data to your custom event" or you want to just use "set"? are you considering the difference? why have you created a custom metric and not a custom dimension? anyway in any case you need to create the variable in Google Tag Manager. Have you created?Real
Trying the same thing here (in the hope to have my custom dimensions applied for automatic events such as scroll/click/...) Had you tested the "real" data and not the Debug View only? Also wondering how we would unset one of the set properties.Ecbolic
S
3

We went through the same issue here: Google Analytics custom dimension not working: gtag set() method issues tl;dr: don't use set, it's for GTM. gtag documentation is misleading.

Swingletree answered 2/3, 2023 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.