0
votes

I replace my current default "universal google analytics" code to custom JavaScript to get the clientId but I got string value while using (Like: clientId).

GA code:

ga('create', 'UA-xxxxxx-x', 'auto'); ga(function(tracker){ var clientId = tracker.get('clientId'); }); ga('set', 'dimension1', clientId); ga('send', 'pageview');

2
dimensions are strings. what did you expect exactly?DaImTo

2 Answers

1
votes

This would hardly work since you're assigning cilentID value to the function scoped variable that can not be seen outside the readyCallback function. Consider the following code:

  ga('create', 'UA-XXXXX', 'auto');
  ga(function(tracker) {
    var cid = tracker.get('clientId');
    tracker.set('dimension1', cid); // ID is to be set right after the traker is available
  });
  ga('send', 'pageview');
0
votes

UPDATE: clientId is available through the API as ga:clientId

No need to set clientId (or userId) in a custom dimension anymore.

Note to anyone using the Universal Analytics User ID feature The values returned in ga:clientId is actually the userId Even more interessting. (As of time of writing) GA fails if you request clientId from a User ID view. So you should use a non-User ID view to get the User ID. :)