5

I have a problem with sending custom events to GA4 using GTM. The problem is the event attributes are dynamically pushed using dataLayer. I can't seem to find a way to reliably forward the attributes to GA4.

So we have a bit of javascript code that runs gtag('event', 'make_noise', {instrument: 'gituar'}).

Then we have a GTM tag "Google Analytics: GA4 Event" that is triggered by "make_noise" custom event above. Now, this works - we can see "make_noise" event being forwarded to our GA4 property.

What I can't figure out is how to forward the "instrument" attribute from the custom event down to GA4. I can't just add instrument: guitar attribute to the GTM tag, as the value of instrument attribute is dynamic. It can be called with wide variety of values, so I should really be able to use whatever is sent via javascript code and forward that to GA4.

So I created a "Data Layer Variable" that reads "dataModel.instrument" and I used this variable in my GA4 Event tag. It worked (dynamic "instrument" attribute is forwarded as expected), but, when I send two events in short timespan, e.g.:

gtag('event', 'make_noise', {instrument: 'gituar'});
gtag('event', 'do_stuff', {foo: 'bar'});

the "make_noise" event on the GA4 side no longer has the "instrument" attribute. It's undefined. Presumably because variable reads last state of dataModel (where only "foo" is defined).

How do I solve this?

6
  • 1
    Can you confirm if you're using gtag or GTM to send the data to GA4? Commented Mar 8, 2021 at 21:40
  • 1
    I'm using GTM to inject UA and configure GA4. Commented Mar 9, 2021 at 20:17
  • If you're using GTM, how are you using gtag()? Did you create custom html or javascript? What's the code that updates the dataModel object? Commented Mar 10, 2021 at 15:20
  • I'm using GTM. gtag function used in the description is a trivial wrapper around dataLayer: gtag = function() {dataLayer.push(arguments);}. Commented Mar 12, 2021 at 15:42
  • Can you provide example code on how the model is being updated? Commented Mar 12, 2021 at 17:53

4 Answers 4

1

Think of your problem as if a client uses the search event and has a dynamic 'search_term' parameter within the event:

  • You have your dataLayer in place

  • Your GTM is setup:

    • Variable: dataLayer variable -> DataLayerVariableName: 'instrument'
    • Trigger: customEvent -> eventName: 'make_noise'
    • Tag: GA4 Event -> eventName: 'make_noise', parameters: {{instrument from DLV}}
  • Set up Analytics under custom events (you should add 'make_noise') and custom definition (you should add 'instrument')

That should be it ...


Edit:

Custom definition of a search_term parameter

Sign up to request clarification or add additional context in comments.

Comments

0

I guess all you have to do is wrap the object keys in [customKey]

Comments

-1

You need to define "instrument" or "foo" as custom parameters in GA4. Or stick to one of the predefined events and their parameters. https://www.optimizesmart.com/understanding-event-parameters-in-google-analytics-4-ga4/

1 Comment

AFAIK it's not needed at all for new parameter to show up in GA4. It's also not the core of my issue. In case I send two events, the first one doesn't even get the value of the parameter sent to GA4.
-1

I was reading your question and suddenly realized that this problem isn't specifically with Google Analytics. While of course it is about Google Analytics, the question is meant to ask, "How do I set dynamic keys in objects?" The way to do this is wrapping the dynamic key in brackets. The code below works.

gtag('event', 'make_noise', {instrument: 'gituar'})

Lets say you wanted to send a custom instrument:

let sender = prompt('Give me an instrument'); //=> Drums
gtag('event', 'make_noise', {instrument: sender})
/*
This becomes:
gtag('event', 'make_noise', {instrument: 'Drums'})
*/

TL;DR But sending a custom key would be done like so:

let customKey = prompt('Give me a key value'); //=> Fruit
gtag('event', 'make_noise', {[customKey]: 'Drums'})
/*
This becomes:
gtag('event', 'make_noise', {Fruit: 'Drums'})
*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.