access universal clientId through GTM

3,746 views
Skip to first unread message

Zvika Jerbi

unread,
Jul 30, 2013, 1:50:52 PM7/30/13
to google-analyti...@googlegroups.com
Hi, 
I cant seem to access GA universal clientId when using GTM. 
I have tried using this code:
anyone with an idea on how this could be solved without accessing the cookie directly?
ga(function(tracker) {
 
var clientId = tracker.get('clientId');
});

Yehoshua Coren

unread,
Jul 30, 2013, 4:36:36 PM7/30/13
to google-analyti...@googlegroups.com
Seems to work for us.  We use the following to pass the clientId into a custom dimension:

ga(function(tracker) {
 ga('set', 'dimension1', tracker.get('clientId'));
});

Zvika Jerbi

unread,
Jul 30, 2013, 4:52:56 PM7/30/13
to Yehoshua Coren, google-analyti...@googlegroups.com
thanks Yehoshua. 
is the basic UA tracking script in your case firing through GTM?



--
You received this message because you are subscribed to a topic in the Google Groups "google-analytics-analyticsjs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-analytics-analyticsjs/rlp07KayUqQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-analytics-ana...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Zvika Jerbi
Tel:               972-3-6253340
Mobile:       972-52-3927675
Email:         zv...@swc.co.il
Web site:       www.swc.co.il

Yehoshua Coren

unread,
Jul 30, 2013, 4:59:03 PM7/30/13
to Zvika Jerbi, google-analyti...@googlegroups.com
Not the regular vanilla UA GTM tag, we're using a custom tag for that.

Zvika Jerbi

unread,
Jul 30, 2013, 5:21:04 PM7/30/13
to Yehoshua Coren, google-analyti...@googlegroups.com
thats why you dont have any trouble getting the clientId. 
the problem starts when using the GTM code predefined in GTM. 
thanks

Yehoshua Coren

unread,
Jul 30, 2013, 5:23:31 PM7/30/13
to Zvika Jerbi, google-analyti...@googlegroups.com
The regular GTM tag doesn't run custom functions.

Zvika Jerbi

unread,
Jul 30, 2013, 5:29:14 PM7/30/13
to Yehoshua Coren, google-analyti...@googlegroups.com
I know. i run the basic code through GTM regular UA tag and trying to access clientId via custom html tag. 

Yehoshua Coren

unread,
Jul 30, 2013, 5:38:07 PM7/30/13
to Zvika Jerbi, google-analyti...@googlegroups.com
Access the clientId via custom html tag which includes a GTM dataLayer.push EVENT. 

Then fire the GTM UA tag with the event macro as a rule.

YC

Ruben F

unread,
Sep 16, 2013, 12:14:59 PM9/16/13
to google-analyti...@googlegroups.com


Here's the solution: 

ga.getAll()[0].b.data.w[':clientId'];

Just run that in the console and check it out. 

Brad Townsend

unread,
Sep 19, 2013, 5:53:00 PM9/19/13
to Ruben F, google-analyti...@googlegroups.com
I'd caution against using the obfuscated field names like 'b', 'data', and 'w'. Those may change from release to release. 

The equivalent command with the official API is: 

  ga.getAll()[0].get('clientId');

This works if you have only one tracker object on the page, but if you have multiple consider setting a Tracker Name in the GTM UI (under More Settings > Advanced Configuration), then accessing the tracker by it's name as follows:

  ga.getByName('myTrackerName').get('clientId');

Cheers,
-Brad




--
You received this message because you are subscribed to the Google Groups "google-analytics-analyticsjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-ana...@googlegroups.com.

Ruben Flam

unread,
Sep 19, 2013, 6:10:11 PM9/19/13
to Brad Townsend, google-analyti...@googlegroups.com
Thanks Brad! 

We figured that one out the hard way.. ;)


2013/9/19 Brad Townsend <btow...@google.com>



--
High5,

RUBEN FLAM
Product Manager

iZettle ®

Владимир Мюге

unread,
Oct 19, 2013, 8:29:23 PM10/19/13
to google-analyti...@googlegroups.com
Hey there!
I spend a lot of time to find solution how to collect clientId in customer dimesion with implement UA tag as UA tag, but not as custom html.

So, the solution: put in value of your dimension macros with macros type "1st party cookie" and cookie name "_ga"

Very hard way for very easy solution, hope it will be helpfull for someone.



2013/9/19 Brad Townsend <btow...@google.com>
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-analyticsjs+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Petr Havlík

unread,
Nov 13, 2013, 10:17:27 AM11/13/13
to google-analyti...@googlegroups.com, Ruben F
Brad, I am getting the error message saying that:

  1. Uncaught ReferenceError: ga is not defined

I tried using Custom Javascript macro to get ClientID, but it doesn't work. Any tips?

Petr
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-analyticsjs+unsub...@googlegroups.com.

Marc Lindsay

unread,
Dec 10, 2013, 8:15:40 PM12/10/13
to google-analyti...@googlegroups.com, Ruben F
I've just had this exact same issue..... finally worked it out (for me)

If you want to see the entire thing play out, in chrome, console.... Just type the following.

tracker = ga.getAll()[0];
clientId = tracker.get('clientId');

No function needed, no ga. needed.

This was using standard GTM setup with UA and running multiple trackers.

I hope this saves someone else the many hours spent researching this sucker.
Message has been deleted
Message has been deleted

Johan Wergelius

unread,
Feb 14, 2014, 3:55:18 AM2/14/14
to google-analyti...@googlegroups.com
WORKING SOLUTION from GTM with custom html setup.

Because you try to get a value that doesn't exist yet when running your code it will not work in GTM without a similar solution as my (might work with jquery .ready function).
It is it working in the console.log because of the the object and values exist at that time.


<script>
function addOnLoadFunction(func) { 
 if (window.attachEvent) {
   window.attachEvent('onload', func);
 }
 else {
   window.addEventListener('load', func);
 }
}

function getClientId () { 
  var tracker = window.ga.getAll()[0],
  clientId = tracker.get('clientId');
  /*
  //The other way
  var clientId = '';
  ga(function(tracker) {
    clientId = tracker.get('clientId');
  });*/
  var dimensionValue = clientId;
  // dimensionValue in ga function can probably be switched to just clientID 
  ga('set', 'dimension1', dimensionValue);
  ga('send', 'pageview');
}

addOnLoadFunction( function() { getClientId(); });
</script>

Maxim Ostapenko

unread,
Mar 1, 2014, 2:05:30 PM3/1/14
to google-analyti...@googlegroups.com
Easy and solid solution, that we are currently using - reading _ga cookie and extracting ClientId from there:

1. Create custom JS macros called "Cookie CId",
2. Enter the following function into custom JS:
function()
{
    var match = document.cookie.match('(?:^|;)\\s*_ga=([^;]*)');
    var raw = (match) ? decodeURIComponent(match[1]) : null;
if (raw)
{
match = raw.match(/(\d+\.\d+)$/);
}
var gacid = (match) ? match[1] : null;
if (gacid)
{
   return gacid;
}
}
3. Use macros {{Cookie CId}} even there is no GA loaded yet on the page.

Johan Wergelius

unread,
Mar 2, 2014, 4:06:56 AM3/2/14
to google-analyti...@googlegroups.com
Yes, Maxim your solution should be the recommended one. Because you don't have to wait it will intergret smooth with all setups in GTM. When using macros it will be easy to both send it to Universal and into your data warehouse etc.

Eduard Schikurski

unread,
Jan 21, 2015, 3:29:53 PM1/21/15
to google-analyti...@googlegroups.com
Awesome solution! I'm using AngularJS with angulartics tagmanager and was now able to set the client-id to a custom dimension without a change to the original code. This wasn't possible with the solution before. Thanks!
Reply all
Reply to author
Forward
0 new messages