9

I'm trying to set up a custom dimension for GA4 called chain_id using the gtag set method. I've also added a custom_map property in the config but I'm not even sure if that is necessary. The issue is that the chain_id is never being sent with the event as you can see in the console output from the GA debugger.

Not really sure what I'm missing here but I think that chain_id with the value of Test chain id should be present in event parameters.

enter image description here

document.getElementById("another").addEventListener("click", function(event) {
  gtag("event", "Button click");
});
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<my-ga-id>"></script>
<script>
  window.dataLayer = window.dataLayer || [];

  function gtag() {
    dataLayer.push(arguments);
  }
  gtag("js", new Date());

  gtag("set", {
    chain_id: "Test chain id"
  });

  gtag("config", "<my-ga-id>", {
    debug_mode: true,
    custom_map: {
      dimension1: 'chain_id'
    }
  });
</script>

<button id="another">Another click</button>
BNazaruk
  • 6,300
  • 3
  • 19
  • 33
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176

2 Answers2

5
  1. gtag set doesn't work. Well or at least it doesn't work as one would expect it to work. Google's documentation is off. Set changes the DL for GTM's benefit. It's not very useful for setting properties. But you have similar options.

  2. You can just set the params within the event call itself, like so:

    gtag("event", "Button click", {chain_id: "test id"});

enter image description here

The picture is from this page's console.

  1. You also can set the eps with the config and they will persist:

enter image description here

Note that Google made another sneak update for ignoring configs that are issued after the initial config has been used. So if you're trying to change persistent event parameters for all subsequent events by issuing a config and gtag.js just ignores it, then give this answer a read: can GA4 custom dimensions be updated after the initial 'config' call?


Overall, please consider using GTM. Or any other TMS. It makes tracking a lot easier to implement, manage, scale and support. Tracking directly with gtag.js only really makes sense when you have to do just a little bit of tracking and you don't really care much about what real business results this tracking achieves in the longer term.

BNazaruk
  • 6,300
  • 3
  • 19
  • 33
  • 1
    My issue is a bit more complicated since I'm actually using another library called `react-ga4` which uses `gtag` under the hood. I've also noticed that its possible with config but it wont work in my case since the chain_id value can change in the app based on user interaction so sounds like the `set` would be perfect for this use-case. I really don't know what is the issue, maybe the `set` implementation in gtag just doesn't work. – Nenad Vracar Sep 27 '22 at 16:37
  • Have you maybe faced the similar issue? – Nenad Vracar Sep 27 '22 at 16:37
  • Well I've played around with set, and I've had a colleague trying it out. We came to the conclusion that gtag has issues with the set. But maybe we're doing something wrong. I looked into set's documentation and no clues there. Just set your eps with every event, I guess. Or stop using react-ga4 and do the tracking in GTM, having react just pushing abstract dataLayer pushes. That would be the best practice. – BNazaruk Sep 27 '22 at 16:42
  • 1
    Thanks for your answer, I will try adding custom dimension to each event as a param and see if that works first. – Nenad Vracar Sep 27 '22 at 17:03
  • Wow, what a mess! I literally can't find a single place on the web where someone is actually using 'set' as described in the documentaion, so maybe you're right and it simply doesn't work. What a disaster! Shame on Google for not making this work. There are good reasons why GTM is not appropriate for everyone. – jerclarke Dec 01 '22 at 02:54
  • 2
    I guess we're just trapped writing a ton of messy code to inject global parameters into every single config and event, rather than setting it once which is logical and what the documentation tells us to do – jerclarke Dec 01 '22 at 02:55
  • No, the best practice would be to use GTM. Using gtag directly is just a hack, essentially. Rarely justified. Similar to using sync blocking IO just to avoid the bother to figure out callbacks and promises. While sure, Google made a documentation mistake there, the set is still to be used in conjunction with GTM connection. Probably a function written exclusively for GTM's sake. – BNazaruk Dec 01 '22 at 15:18
  • 1
    The best practice is to follow Google's documentation. Using gtag directly is supported and there are very good reasons to do so, even if you can't imagine them. Telling people to use GTM is gaslighting, and it isn't answering the question, so it's not appropriate on SO. – jerclarke Dec 01 '22 at 16:21
  • It does work. And you can use it. It's just a bad practice. And no, it's not gaslighting. can != should. – BNazaruk Dec 01 '22 at 16:24
  • 1
    So your argument is Google is promoting bad practices on their official docs. If so that is bad and they should feel bad and we should be very angry with them. I disagree though. The error is not making it clear what works and what doesn't. Everything they document should be considered a good practice unless constantly warned against. – jerclarke Dec 01 '22 at 16:26
  • 1
    And to be clear, using gtag() works fine, yes, but not using 'set', which is the point of this and many other SO questions from people baffled at the scope of Google's error. – jerclarke Dec 01 '22 at 16:26
  • It's not like they're promoting it. It's an option. And sometimes it's valuable. It's like saying that npm promotes bad practices by allowing sync io packages. gtag may be valid in cases when you can't use GTM. I'm afraid I don't see straightforward examples of where gtag would be a better solution. Maybe in chrome extensions. Maybe for companies that prohibit TMSes completely. – BNazaruk Dec 01 '22 at 16:30
  • Hi @BNazaruk sorry I am asking this question so late. Is the custom map inside the config really needed if we don't want the custom dimensions to be sent with every event/persisted in every event we send ? As you mentioned my issue is with the reconfiguration of the google tag..thats why i am trying to avoid this custom map and just send the custom dimensions normally in the event. – Srikar Rao Jun 15 '23 at 07:59
  • It's not necessary, but it's very comfy. Issues with reconfiguration are solved by changing the config rewriting setting in GA4 admin. Still, sure. You can set all the properties explicitly in every event and it will work. They won't persist past the event, but it's expected. – BNazaruk Jun 15 '23 at 16:04
0

I am not sure why set is not working. But maybe you can try to use config

Google Document about it


When we use GTM to do something like this. We will use Field To Set and it's basically call gtag('config','{Your GA4 ID}', { 'chain_id': 'Test chain id' });

And this will need some additional code when you call event

gtag('event','click', { 'send_to': '{Your GA4 ID}' });

I think it is worth to try.

darrelltw
  • 1,734
  • 1
  • 10
  • 21