gtag set is not attaching data as expected
Asked Answered
H

3

11

I'm trying to follow this guide: https://developers.google.com/gtagjs/reference/api#set

It says that you can use gtag('set', {key: value}) to add a set of values to the next gtag calls. However, it doesn't work.

So, this is the setup I used in order to have gtag available on the app:

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('set', 'anonymizeIp', true);
gtag('config', 'my_ga_tracking_id', {
  send_page_view: false,
  custom_map: {
    dimension1: 'a_dimension' 
  }
});

And then this code once the app is loaded:

gtag('set', { a_dimension: 'test' });
gtag('event', 'an_event');

I expected to see the a_dimension attached to the event an_event sent to GA but I can't find it.

What am I missing?

Hidebound answered 28/1, 2021 at 10:24 Comment(4)
have you set this variable in Tag manager itself?Changeover
No. Is that required?Hidebound
So as far as I know yes. I recently did some Gtag stuff and to get it to pass into GA, I had to create the variable, and pass it in to an event triggerChangeover
Actually, I defined a dimension, not a tag, on GA. Does it count?Hidebound
H
3

I solved by using gtag('config') where I needed to set common dimensions instead of gtag('set').

The only think I dislike in this solution is that I need to have those common values always available at that level.

Hidebound answered 29/1, 2021 at 9:25 Comment(2)
Another issue with this is that its not really dynamic in case the dimension data changes during the user interaction with the app. As far as I can see once you call config with some values you can't change it later onAgnostic
Using the set command differs from passing values directly to the event command. When you pass values directly to an event command, those values only apply to the event being fired. But with set, the values persist on the current page and are passed with all subsequent events. developers.google.com/tag-platform/gtagjs/reference#setSpondee
S
1

In short: don't use set. It's meant for GTM. gtag documentation is misleading in this regard. We went more thoroughly through it here: Google Analytics custom dimension not working: gtag set() method issues

Subgenus answered 2/3, 2023 at 17:20 Comment(0)
G
0

to add properties you need to use 'set' together with 'event'. And also configure Special definitions in GA.

For example

const handleUserPropertiesNePetro = () => {
    gtag('set', 'user_properties', {
      custom_client_character: 'Halerik'
    });
    gtag('event', 'user_properties');
  };
Gaekwar answered 18/10, 2023 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.