Reply
Highlighted
Frequent Visitor
Posts: 18
Registered: ‎11-10-2017
Accepted Solution

Get Google Analytics Client ID and map to a GA custom dimension

I am using Tealium for my Google Universal Analytics tagging, and need to retrieve the value of the GA-assigned "Client ID", and map that back to GA as a value in a custom dimension. I see several articles on how to do this in Google Tag Manager, such as this one https://www.simoahava.com/gtm-tips/use-customtask-access-tracker-values-google-tag-manager/ or this one https://optimizationup.com/expose-client-user-id-google-analytics/. I need to do essentially the same thing, but using Tealium. Can anyone recommend a way to accomplish this?

Reply
0 Kudos
Tealium Employee
Posts: 765
Registered: ‎09-01-2015

Re: Get Google Analytics Client ID and map to a GA custom dimension

@Krasner

Here is a function I've used leveraging Google's built-in tracker.

Add this to a JavaScript extension scoped to All Tags.

ga(function() {
var trackers = ga.getAll();
for (var i = 0; i < trackers.length; ++i) {
var tracker = trackers[i];
var trackerName = tracker.get('name');

ga(function(trackerName) {
utag.data._ga = tracker.get('clientId');
});
}
});

 

Declare _ga as a UDO variable in the Data Layer tab.

Then map _ga to your custom dimension within the tag mappings.

Please let me know if this works for you.

Cheers,
-Dan

Frequent Visitor
Posts: 18
Registered: ‎11-10-2017

Re: Get Google Analytics Client ID and map to a GA custom dimension

[ Edited ]

Thank you, @dan_george, for the detailed solution. I think I am quite close to getting this to work. I now see the value fo the GA Client ID showing up in the new UDO variable called "_ga" in the Tealium Web companion. However, even though I have mapped it in the Google Universal Analytics tag to the custom dimension, it does not appear to be getting populated. Perhaps there might be a timing issue? I have the extension set to "After Load Rules (default)". And I have the GUA tag set to Bundle=ON, Wait=OFF. Wondering if perhaps the way things are set up, the variable might not have the data by the time the tag references it? Any thoughts?

Reply
0 Kudos
Tealium Employee
Posts: 765
Registered: ‎09-01-2015

Re: Get Google Analytics Client ID and map to a GA custom dimension

@Krasner

I would scope the extension to the GA tag as this will ensure the logic is ran in time and also when the GA lib is loaded.

Reply
0 Kudos
Frequent Visitor
Posts: 18
Registered: ‎11-10-2017

Re: Get Google Analytics Client ID and map to a GA custom dimension

Dan, again thanks. I thought the hard part would be getting the client ID value. Your solution readily solved that. For some reason, I have not been successful to map the value to the dimension in the Google Universal Analytics tag. I'll reach out to my CSM or to support to see if we can get it that last inch over the goal line, and will update here. Thanks for your help getting me this far.

Reply
0 Kudos
Frequent Visitor
Posts: 18
Registered: ‎11-10-2017

Re: Get Google Analytics Client ID and map to a GA custom dimension

[ Edited ]

Hi @dan_george. My CSM helped me determine that the code you provided works properly if I define the _ga variable as a "first party cookie", rather than as a "UDO Variable".

However, when I do that, it seems that a few extra characters are prefixed onto the data value in the cookie for some reason. Like "GA1.2." and then followed by the actual value of the ClientID.

What would I need to change in order for this to function as a UDO variable type of variable instead of as a cookie-type variable? Is it just a matter of how the variable is referenced (utag.data._ga)?

Reply
0 Kudos
Visitor
Posts: 27
Registered: ‎03-09-2016

Re: Get Google Analytics Client ID and map to a GA custom dimension

Hi @Krasner,

we have implemented this as well, so maybe I can help. The reason why changing the variable type to cookie-type works is because you then read the cookie which GA uses to store the client id directly. However, as this an implementation detail its direct usage is not recommended by Google (https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the...) and you should use the api provided by the ga object to get the client id.

You can use the solution provided by Dan, but as the function can only run when the ga object is initialized the pageview for the current page is already sent. This means that you have to fire a second hit to ensure that the client id is sent to GA. To prevent a bloating of our GA hits we chose to use the solution provided in your first post via the custom task and to modify the GA tag template (as there is currently no mapping for the custom task in the template).

We added the follwoing code to the loop in the initTracker() function:

u.o(c.name + '.set', 'customTask', function(model) {
    model.set('dimension1', model.get('clientId'));
});

We would prefer not having to modify the template, but as long as Tealium does not provide a mapping for the custom task we think it is the best solution for us.

Best regards,

Andreas

 

Employee Emeritus
Posts: 1,019
Registered: ‎08-26-2015

Re: Get Google Analytics Client ID and map to a GA custom dimension

Great stuff @ahrasch!

Remember to give me a kudo if you like my post! Accepting my post as a solution is even better! Also remember that search is your friend.
Reply
0 Kudos
Frequent Visitor
Posts: 18
Registered: ‎11-10-2017

Re: Get Google Analytics Client ID and map to a GA custom dimension

[ Edited ]

After working with Tealium support, we implemented a variation of the solution @ahrasch provided. As described, we used a custom task. However, we did this in an extension, rather than by modifying the tag template. And to supplement this, for the possible occasions where timing causes the tag to fire before the value is provided, we are also storing the value in a session cookie. In this solution, there is still some possibility of the Client ID not being captured on the very first pageview hit, but so far we are seeing it capture 100%, and we also know that most of our pages have at least one event hit after the pageview.

So, the solution involves creating one first party cookie variable (as utag_main_gaClientId), mapping this to the custom dimension in the tag (we used custom dimension 9, after creating this in our GA property settings), and then creating the following javascript code extension, scoped to our GUA tag only:

ga(function() {
var trackers = ga.getAll();
for (var i = 0; i < trackers.length; ++i) {
var tracker = trackers[i];
var trackerName = tracker.get('name');

ga(function(trackerName) {
utag.data['client_id'] = tracker.get('clientId');
utag.loader.SC("utag_main", {"gaClientId": utag.data['client_id']+";exp-session"});
ga(tracker.get('name') + '.set', 'customTask', function(model) {
model.set('dimension9', model.get('clientId'));
});

});
}
});

Employee Emeritus
Posts: 1,019
Registered: ‎08-26-2015

Re: Get Google Analytics Client ID and map to a GA custom dimension

Oustanding solution @Krasner and @ahrasch! Thank you both for sharing!

Remember to give me a kudo if you like my post! Accepting my post as a solution is even better! Also remember that search is your friend.
Reply
0 Kudos
Visitor
Posts: 1
Registered: ‎12-05-2017

Re: Get Google Analytics Client ID and map to a GA custom dimension

Hi @ahrasch

any idea how you would translate this code to work with the new gtag.js.

it does not seem to have an initTracker function that references the tracker!

Thank you

Reply
0 Kudos
Visitor
Posts: 27
Registered: ‎03-09-2016

Re: Get Google Analytics Client ID and map to a GA custom dimension

Hi @salih

I am not familiar with the new gtag.js, as I don't think it is very useful when already using a tag manager. Also, it does not seem to be feature complete yet in comparison with the old analytics.js. The custom task used in my solution is not mentioned in the gtag.js documentation, so I don't know if it is supported at all.

Best regards,

Andreas

Visitor
Posts: 1
Registered: ‎06-13-2018

Re: Get Google Analytics Client ID and map to a GA custom dimension

Hi @ahrasch,

Where in the initTracker() loop did you add that code?  I've placed it at the very end of the loop; is that the correct place?  I've attached the picture here:

Tealium Client ID Tracking.PNG

Hope to hear from you soon, thanks!

- Dustin

 

 

Reply
0 Kudos
Visitor
Posts: 27
Registered: ‎03-09-2016

Re: Get Google Analytics Client ID and map to a GA custom dimension

Hi @DChen721,

this is exactly how we did it. You can only set properties on the ga object after the tracker has been created, so it has to be at the end of the loop.

Best regards,

Andreas

 

P.S. You maybe should also check your version of the GA tag template. In our case, the adddition is around line 170. So it seems the tag template you are using is quite old and maybe you want to update it before adding the code (see https://community.tealiumiq.com/t5/iQ-Tag-Management/Managing-Tag-Templates/ta-p/21713).

Reply
0 Kudos