简体   繁体   English

Google Analytics空客户端ID

[英]Google Analytics empty clientID

Why sometimes I get empty client ID ? 为什么有时候我会得到空客户端ID? For example for ~90% of my page visitors Google Analytics set clientID normally, I'm checking it by: 例如,约90%的网页访问者Google Analytics正常设置了clientID,我通过以下方式检查:

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

and it returns some xxxxx.xxxxx number. 它返回一些xxxxx.xxxxx数字。 But for ~10% of visitors it return empty value. 但对于约10%的访客来说,它会返回空值。 why? 为什么?

Im checking if ga is loaded by: 我通过以下方式检查ga是否被加载:

if (typeof ga === 'function')

And it seems that its ok. 似乎没关系。 My whole function looks like: 我的整个功能看起来像:

function check_ga() {
  var clientId = "";
  if (typeof ga === 'function') {
    ga(function(tracker) {
      clientId = tracker.get('clientId');
    });

    if(clientId == "") 
    {
      ga('create', 'UA-XXXXXX-X', 'auto'); //trying to create new ga object
      ga(function(tracker) {
        clientId = tracker.get('clientId');
      });
    }

    if(clientId == "") 
    {
      clientId = "No CID";
    }
  } else {
    clientId = "GA Not loaded";
  }
  return clientId;
}

And time to time I get "No CID" value. 并且我不时获得“无CID”值。 Why?? 为什么?? As you see, im trying to create new GA object, but it is not working also. 如你所见,我试图创建新的GA对象,但它也不起作用。

When invoking the ga command queue, functions that are passed in are only executed as soon as the analytics library is completely loaded. 调用ga命令队列时,只有在完全加载分析库后才会执行传入的函数。 In your code, you queue up your callback function to evaluate the client id and assign it to your variable, but when you determine if it is empty or not, it is in fact still empty because the analytics library still has not completely loaded, thus resulting in the "No CID" being assigned. 在您的代码中,您将回调函数排队以评估客户端ID并将其分配给您的变量,但是当您确定它是否为空时,它实际上仍然是空的,因为分析库仍然没有完全加载,因此导致分配“无CID”。

Cf. 参看 GA command queue GA命令队列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM