Google Tag Manager: UserId activate without login needed

by Luca Rabboni


One of the most interesting innovations brought by Universal Analytics is the UserId, which opens new horizons in terms of user behavior and cross-device tracking.

UserId tracking usually requires a login ID systems or CRM customer IDsince GA cannot automatically generate a random ID. In some cases (and our blog is an example), these features are not available, and you need a “custom script” in order to generate user ID and track unique users.

We began starting from Simo Ahava post but we found two critical issues for our site:

The post included the use of four macros of which one was a custom javascript to intercept if the user had or not the GA cookie. Macros were fired at each tag, so the function was called about 6 times. In practice, users from “Returning Visitors” became New Visitors”.

As you can see in the script below (the “start_timer”) was called at least 4 timesrewriting the value everytime.



Debug Della funzione

GTM – Debug with dataslayer for UserID

After many tests and researches, we came across this post by Dan Russell suggesting to take the ID directly from Google Analytics _utma using only a Custom HTML Tag.


Below you find the process we followed:


1) Creating a Custom HTML tags containing the following script:


The code read the _utma value. Since the _utma is split by points .”, the script divides the value of the cookie and takes only the Random ID which corrispond to the third element of the string (see image below)


random part id from utma

random part id from utma

NOTE: In this step is crucial that the script is fired before the pageviews tag, which in our case was linked to the custom dimension that we wanted to pass to Google Analytics. To do this, simply set the fire tag priority (a high number means higher priority).


The extracted value is passed to a variable,

and finally sent either as custom dimension and as  event. (We chose to insert only the custom dimension)

Interface side we only had to create aDataLayer Variable”


And pass the variable as a custom dimension in Google Analytics. The custom dimension we are talking about is the N 7.

GA custom dimensions

GA custom dimensions

The uid field instead is what will allow Google Analytics to populate the view Unified Sessions “. Now the only thing left is to set the interface of Google Analytics as follows:


Google analytics Custom_Dimension_Setup

Google analytics Custom_Dimension_Setup


Et Voila, you’re done! You can see data in Sessions Unified” view created by Google Analytics or  as a custom dimension in the standard reports (just selecting userId as a secondary dimension)