Jayy
Jayy

Reputation: 93

Google Analytics (GA4) not reading userId from Google tag manager datalayer variable

I am developing a website using nextJs. While implementing GTM and Google analytics, I ran into a problem where GA4 is not reading user_id property from Google tags manager..

My code is pushing the event and firing the tag: Firing GA4 config tag

The Tag is reading correct value. GA4 tag values

and here are the following datalayer variables. Datalayer variables

When I check the GA4 debug view, I cannot see the user_id being set. The User Properties tab stays empty and nothing is coming in that fills it up..

I have tried using first party cookies to set user_id which actually worked, but the issue I faced then is when logging the user out and clearing said cookies, GA4 still managed to keep the user_id saved and not clear it from future events.

Can someone point me in the right direction please?

Upvotes: 1

Views: 2397

Answers (4)

John Harding
John Harding

Reputation: 457

For anyone using the react-ga4 npm package, I solved this by removing the gtag("config", "YOUR-GTAG-ID") code included in the script tag in your index.html file, and added it to my the top of my first JS file with:

import ReactGA from "react-ga4";

ReactGA.initialize("YOUR-GTAG-ID", {
    gaOptions: {
        userId: 'user-id',
    }
});

Upvotes: 0

DraftDodger
DraftDodger

Reputation: 21

I found a solution to this issue. By default, Google Tag Manager is configured to ignore duplicate config calls. And your dataLayer variable is not yet initialized when the first GA4 Config is called during the page initialization. If you turn off this flag and trigger the config tag again with user_id, it'll work like a charm. Check out the documentaion here

Upvotes: 2

osi_okorie
osi_okorie

Reputation: 73

It's not working because your auth event tag gets fired after container has loaded. You can fix this by making sure you push to the datalayer before the declaration of your google tag manager container.

I pushed my variable before declaring my container in line 16.

enter image description here

Upvotes: 0

user7203546
user7203546

Reputation:

I'm facing the same problem, and researched about it. I'm suspicious now that "GA4 Configuration" tag will be work only once and setting user_id field too no matter how many I trigger it. I think the only way is reloading the page to refresh GTM tag, but I'm not satisfied with this conclusion, so I'll be back when I find a better way.

Upvotes: 0

Related Questions