javascript - can GA4 custom dimensions be updated after the initial 'config' call? - Stack Overflow

admin2025-04-21  0

I have google analytics 4 being loaded via GTM, and i've been struggling with adding custom dimensions to the "enhanced measurement" events. after reading some other answers i've determined that yes, the dimensions work when set in the 'config' call for the tracking id (which i can do in GTM under "fields to set"), but 'config' seems like it noops on subsequent calls for an already configured tracker with GA4 (with UA it does something).

as discussed in the other questions, 'set' operations don't seem to do anything, and i'm not manually dispatching the "enhanced collection" events so i can't manually specify the parameters in the call.

is there any way to add parameters to "enhanced measurement" events after the tracker is configured?

a minimal example:

gtag('set', {'dimension1': 'test1'})
gtag('config', 'G-XXXXX', {dimension2: 'test2'})
gtag('set', {'dimension3': 'test3'})
gtag('event', 'test', {send_to: 'G-XXXXXX', foobar: 'baz'})

the debugger shows the event parameters: en: test _ee: 1 ep.dimension2: test2 ep.foobar: baz

on a page navigation the automatic enhanced collection event looks like this: en: page_view ep.dimension2: test2 _et: 922

however when using an old GA account:

gtag('config', 'UA-XXXXXX')
gtag('event', 'test', {send_to: 'UA-XXXX', foobar: 'baz'})

it shows all the set dimensions: "dimension1": "test1" "dimension3": "test3" "&jsscut": "1" "hitCallback": [function] "hitType": "event" "eventCategory": "general" "eventAction": "test"

why does the G4 integration not pick up on set dimensions? and is there any way to modify dimensions after the tracker is configured other than including them on the event call

I have google analytics 4 being loaded via GTM, and i've been struggling with adding custom dimensions to the "enhanced measurement" events. after reading some other answers i've determined that yes, the dimensions work when set in the 'config' call for the tracking id (which i can do in GTM under "fields to set"), but 'config' seems like it noops on subsequent calls for an already configured tracker with GA4 (with UA it does something).

as discussed in the other questions, 'set' operations don't seem to do anything, and i'm not manually dispatching the "enhanced collection" events so i can't manually specify the parameters in the call.

is there any way to add parameters to "enhanced measurement" events after the tracker is configured?

a minimal example:

gtag('set', {'dimension1': 'test1'})
gtag('config', 'G-XXXXX', {dimension2: 'test2'})
gtag('set', {'dimension3': 'test3'})
gtag('event', 'test', {send_to: 'G-XXXXXX', foobar: 'baz'})

the debugger shows the event parameters: en: test _ee: 1 ep.dimension2: test2 ep.foobar: baz

on a page navigation the automatic enhanced collection event looks like this: en: page_view ep.dimension2: test2 _et: 922

however when using an old GA account:

gtag('config', 'UA-XXXXXX')
gtag('event', 'test', {send_to: 'UA-XXXX', foobar: 'baz'})

it shows all the set dimensions: "dimension1": "test1" "dimension3": "test3" "&jsscut": "1" "hitCallback": [function] "hitType": "event" "eventCategory": "general" "eventAction": "test"

why does the G4 integration not pick up on set dimensions? and is there any way to modify dimensions after the tracker is configured other than including them on the event call

Share Improve this question edited May 10, 2023 at 20:02 tomwoodward asked May 4, 2023 at 20:44 tomwoodwardtomwoodward 1056 bronze badges 3
  • How about configuring it again, but asking configure not to send the pageview? Just do a silent reconfig. Though it's odd to me that you can config and yet don't send the EEC event. Does an plug-in send it? No GTM? – BNazaruk Commented May 6, 2023 at 6:51
  • i updated my question with a more minimal example, to answer the first part of your question, calling config again doesn't seem to do anything or affect future events, with or without send_page_view specified – tomwoodward Commented May 10, 2023 at 20:04
  • I'll try to find out if there's a way to update the persisted dimensions, but would you be ok doing it through GTM? Sending the data to GTM (through dataLayer []) and having it setting the dimensions? This is also the best practice since it limits any data setting to GTM, making it way easier to debug. It seems to me a dangerous mess to send events through both gtag() and gtm. – BNazaruk Commented May 10, 2023 at 21:37
Add a ment  | 

2 Answers 2

Reset to default 6

Okay, dug the issue up. Google is a bit odd with how they push new stuff related to GA4. Somewhere last August Google rolled out a new feature. To ignore 'duplicate' config calls. And enabled it by default. While the idea of it seems a bit ridiculous, it bees even more ridiculous when you realise it was a sneak update without notifying us or documenting it.

Anyhow:

  • Go to your GA4 property’s Admin
  • Data Streams
  • (your data stream)
  • Configure Tag Settings
  • Your Google Tag and
  • Click the box on the left for your stream
  • Toggle OFF “Ignore duplicate instances of on-page configuration.”

source

Yes, gtag.js' behavior is configured depending on the config of your GA4 property. There are more things you configure in your library through this ui. One of which would be cross-domain linking rules.

Thanks to Angela Grammatas and Todd Bullivant for helping figuring this out on Measure slack :)

Other than that, it's a bit odd to use both GTM and gtag() in parallel. Will lead to gathering of technical debt. It's much better to migrate all direct gtag() calls to GTM via dataLayer.

in addition to the correct info from @bnazaruk(that is STILL undocumented), I also found that subsequent calls to gtag('config',...) overwrite values from previous calls instead of merging them. The documentation seems to suggest that they are merged rather than replaced and I feel like that was what it used to do a few years ago when I last had to set something like this up. To keep previous config objects I also had to do this

const oldConfig =
  window.dataLayer.find((args) => args['0'] === 'config')?.['2'] ?? {};
gtag('config', '<GA_KEY>', {
  ...oldConfig,
  new_prop: 'blahblah'
});

which only adds in the first instance of a config call since that's the only one I care about, but you could easily change the find call to a filter or reduce to get all of them

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745177441a288992.html

最新回复(0)