5

I have setup a new Universal Analytics tracking and would like to store ClientID in one of the custom dimensions slots.

The tracking tags are implemented through Google Tag Manager (using the predefined UA tag) and I am having the difficulty to passing the Client ID (via custom JavaScript Macro) with the basic pageview request.

I have named my tracker so that I can access it and this code runs without any error in console, however when used with GTM, it's returning an error.

ga.getByName('trackerGTM').get('clientId'))

produces an error:

Uncaught ReferenceError: ga is not defined

Any tips?

3 Answers 3

9

I assume you're firing the pageview {{url}} matches RegEx .*, correct? Could be that Universal hasn't loaded yet. Have you tried {{event}} equals gtm.dom? I haven't tested this, but it's a thought.

EDIT:

This looks like a bug. I was able to run:

function(){
  var client = ga.getAll()[0].get('clientId');
  return client;

}

And have it spit out the client ID with no problem.

6
  • Blexy, have you used custom JS macro for this? I did exactly the same thing and am still getting undefined. Commented Nov 16, 2013 at 16:42
  • Yeah, custom JS macro. However I did it with an unnamed tracker. I think there is a bug with trying to getByName using GTM. Is it possible to have unnamed tracker?
    – Blexy
    Commented Nov 16, 2013 at 17:25
  • Blexy, I am still getting undefined, but I think this might be related to unconventional GTM implementation in general (not directly in source code due to the CMS limitation).I will try to fix it with our development team Commented Nov 18, 2013 at 13:18
  • 1
    So Blexy I finally got back to this and found out that the issue is related to having the tracker object ready. I setup an event that is triggered once "event" equals to "gtm.load" and the value is passed on correctly. Thanks for the input once more :) Commented Jan 27, 2014 at 15:35
  • Ah, sounds like a race condition - macro was running prior to ga was loaded. Makes sense. Thanks for the follow up - this is good to know!
    – Blexy
    Commented Jan 27, 2014 at 17:14
1

The real problem is that GTM (Google Tag Manager) creates an object that is NOT called ga (it creates an object named ga followed by a random number), that's why 'ga' is not defined. You should try something on the lines of using the tag manager to put that client id into a javascript variable so you can access it from the page)

2
  • Please post more details
    – adao7000
    Commented Jul 27, 2016 at 22:16
  • I haven't done that, but once I had a similar issue and had to put a mixed solution (using GTM and UA code for cross domain tracking). I know it could be done entirely (or mostly) using GTM but time was an issue. Regards Commented Jul 29, 2016 at 4:00
0

I was reading up on setting client ids and came across this blog link about accessing client ID in GA while using Google Tag Manager. Might be helpful.

Accessing Client ID in GA using custom dimensions

Not the answer you're looking for? Browse other questions tagged or ask your own question.