Search code examples
javascriptgoogle-analyticsgoogle-analytics-firebasegoogle-analytics-filters

How to get clientId with custom dimensions in google analytics


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');


Solution

  • 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');