javascript 如何可靠地获取 Google Analytics 生成的会话 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26915432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:40:36  来源:igfitidea点击:

How to reliably get session id generated by Google Analytics?

javascriptgoogle-analyticsgoogle-analytics-api

提问by adam0101

Using the latest analytics.js code (as of today) is there a way through their JS API I can get the session id?

使用最新的 analytics.js 代码(截至今天)有没有办法通过他们的 JS API 获取会话 ID?

采纳答案by Philip Walton

If you're talking about the client ID, you can get that as follows:

如果你在谈论客户端 ID,你可以得到如下:

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

Here's where that's stated in the documentation:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie

这是文档中说明的地方:https:
//developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie

Keep in mind that the client ID is (usually) stored in a cookie, so if the user clears their cookies, they'll be assigned a new client ID that may not match the one you have.

请记住,客户端 ID(通常)存储在 cookie 中,因此如果用户清除他们的 cookie,他们将被分配一个新的客户端 ID,该 ID 可能与您拥有的不匹配。

回答by Don Rhummy

You can read the cookie created by Google but:

您可以读取由 Google 创建的 cookie,但是

  1. It's a privately (by google) chosen variable name/format and could change at any time, thus breaking your code
  2. It appears to break their TOS if you store that information
  1. 这是一个私下(由谷歌)选择的变量名称/格式,可以随时更改,从而破坏您的代码
  2. 如果您存储该信息,它似乎会破坏他们的 TOS

https://webmasters.stackexchange.com/questions/51866/can-you-track-one-users-session-with-google-analytics-by-using-the-utma-cooki

https://webmasters.stackexchange.com/questions/51866/can-you-track-one-users-session-with-google-analytics-by-using-the-utma-cooki

Linking Google Analytics event logs to unique session ID

将 Google Analytics 事件日志链接到唯一的会话 ID

http://www.analytics-ninja.com/blog/2011/08/how-google-analytics-calculates-visits.html

http://www.analytics-ninja.com/blog/2011/08/how-google-analytics-calculates-visits.html

回答by Dmitry Naumov

Be aware! Some ad-blockers like uBlock Originreplaces GA script with its own polyfill.
This means that you can't correctly check is it real GA script are loaded on your page.

意识到!一些广告拦截器(如uBlock Origin)用自己的 polyfill 替换了 GA 脚本。
这意味着您无法正确检查页面上是否加载了真正的 GA 脚本。

Also uBlock's polyfill changes GA API, so when you using ga(function(tracker){})callback will never called!
So don't relay on callback and never wait for its calling

此外uBlock的 polyfill 更改了 GA API,因此当您使用ga(function(tracker){})回调时将永远不会调用
所以不要中继回调,也不要等待它的调用

The better way is to manually store custom clientIdin cookie and tell this clientIdto GA https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=en#clientId

更好的方法是clientId在 cookie 中手动存储自定义并将其告诉clientIdGA https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=en#clientId