Search code examples
google-analyticsgoogle-tag-managergoogle-analytics-4

Send User ID from Google Tag Manager to Google Analytics 4


I have set up google tag manager with my GA4 property
(NOT legacy Universal Analytics)
according to the official guide:
https://support.google.com/tagmanager/answer/6103696?hl=en&ref_topic=3441530#zippy=%2Cweb-pages

and I have figured out how to send custom/recommended events to GA4 using
dataLayer.push({}) objects, using their "event" field in triggers and tags,
based on a Universal Analytics guide
(https://www.analyticsmania.com/post/track-logins-with-google-tag-manager/),
because this appears to be a black hole in the official guide:
https://support.google.com/tagmanager/answer/9442095
only describes, briefly, the GTM-side of the story.

Same goes for sending User ID: the docs only tell
when creating a Google Analytics 4 Configuration tag:

To set a user ID, add a row to Fields to Set. 
Set the Field Name to user_id, and the Value to 
a Tag Manager Variable that returns the user ID.

and I only take the knowledge from the external UA guides
to use a Data Layer Variable that reads the user_id field from the dataLayer,
and also create a corresponding user_id User Property on Google Analytics, which in Universal Analytics used to
have related settings that don't exist anymore (https://www.analyticsmania.com/post/google-analytics-user-id-with-google-tag-manager/).

So I have deployed my GA4 Events with the user_id data layer variable
referenced in their Configuration (and it is visible on the datalayer in Debug View), created the corresponding User Property in GA, and enabled User-ID reporting.
Yet, in my real-time view, the user_id fields always get some weird "gtm.js" value
(as if my data layer variable value was replaced by the event name
from the default dataLayer.push({event:"gtm.js",user_id:"ignored value")).
What in the world is missing for my user_id field to be recognised? user_id in debug view

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Thanks!


Solution

  • So at long last it turned out, that since the configuration tag was fired by the Page View event, that happened before I pushed the user_id to the dataLayer, so it filled the non-existing value with the event name instead.

    I gave the user_id event a name to create a custom event trigger, that re-triggers the configuration tag. This way the user_id-s are finally visible.