4.8K members online now
4.8K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
Guide Me

GTM and userId tracking from datalayer variable

[ Edited ]
Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆


I'd appreciate any help in solving the following issue. I have GA implemented via GTM the most standard way. I also need to pull in userId with all the settings in the property ready. The userid is available in datalayer variable but only after some events fire on the website after GA code is already loaded. 

I couldn't find any solutions to pull in userId in this simple scenario. How can we bind it with GA if it becomes available only after GA is already loaded?

GA has this method: 

// At a later time, once the `userId` value is known,
// sets the value on the tracker.
('set', 'userId', USER_ID);

But I couldn't implement it via GTM... the custom HTML TAG is not an option ... GTM assigns names to all GA trackers.

Also, it's not clear how to check whether the userid data was sent to servers or not... where should we see it in the console or reports in real time?

1 Expert replyverified_user

GTM and userId tracking from datalayer variable

Participant ✭ ✭ ✭
# 2
Participant ✭ ✭ ✭

Hi Yuri,


Here you can find a couple of great articles that help you implement user-ID tracking via GTM:






Hope this helps. Let us know how it goes.



GTM and userId tracking from datalayer variable

Follower ✭ ☆ ☆
# 3
Follower ✭ ☆ ☆

It seems the only solution is to define dataLayer before GA-pageview is fired, it means the dynamically generated DataLayer code has to be placed before GTM script...

GTM and userId tracking from datalayer variable

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭


GTM and userId tracking from datalayer variable

Top Contributor
# 5
Top Contributor

Hi @Yuri S either push the data (render data) before the GTM snippet or use a slightly different approach by adding using the push method and a custom event for instance:

dataLayer.push({'event':'setUserId', 'userId': 'A123'});

You would then have to just create a new GA template tag and send the payload via an event as and option AND create a Custom HTML tag which would control a cookie - set cookie. The cookie can have a name of userId and the value would be retrieved for each GA tag as a 1st party cookie variable.


The cookie should expire after session ends or after the user explicitly logs out.


Make sure session unification inside GA property admin is on.


Hope it helps.


Kind regards,




Web analyst @