how to get the Google analytics client ID

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

When you are creating a new instance of analytics.js by running

ga('create', 'UA-XXXXXXX-Y', {'cookieDomain': 'none'});

GA creates a unique client Id. I want to fetch this id and use it for my own purposes, but I can find only setter for this parameter but can't find any getter method to get it.

GA send it later in a parameter called &cid=123123.232323

Does anyone knows how do I get it?

回答1:

Google does have some documentation on getting the client id.

Looks like this:

ga(function(tracker) {   var clientId = tracker.get('clientId'); }); 

I've used this before, too:

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


回答2:

First create the Google Analytics ga object to create a tracker object, by passing it a "Ready callback" function, then use the tracker to call other methods.

The ga() command queue provides an interface for doing almost everything you need to do with the analytics.js library.

"function(tracker)" is a callback function to be invoked when the analytics library is fully loaded and ready to be interacted with. The function is invoked with the default tracker object as it first argument. If no default tracker has been created, the first argument is/will return undefined.

Note: when the callback function is invoked, all ga object methods are available for use. Including the one you want tracker.get('clientId')

Replace the UA-XXXXX-Y in the code below with your UA code from Google Analytics.

// Queues a tracker object for creation. ga('create', 'UA-XXXXX-Y', 'auto');  // Once the tracker has been created, log the // client ID to the console. ga(function(tracker) {   console.log(tracker.get('clientId'));   /* Your other code here */ }); 

Alternatively for lines 1 & 2, use the code below to create a named tracker.

// Queues a named tracker object for creation. ga('create', 'UA-XXXXX-Y', 'auto', 'myTracker'); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!