如何将“安全"设置为“安全". Google Analytics(分析)全球网站代码(gtag.js)使用的Cookie的属性? [英] How to set "secure" attribute of the cookies used by Google Analytics Global Site Tag (gtag.js)?

查看:92
本文介绍了如何将“安全"设置为“安全". Google Analytics(分析)全球网站代码(gtag.js)使用的Cookie的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Github页面上使用了Google Analytics(分析)全局站点代码(gtag.js)跟踪代码.以下跟踪代码作为第一项复制并粘贴到我要跟踪的每个网页的<HEAD>中(我将自己的跟踪ID替换为GA_MEASUREMENT_ID):

I'm using Google Analytics Global Site Tag (gtag.js) tracking code on my Github Pages. The following tracking code is copy-and-pasted as the first item into the <HEAD> of every webpage I want to track (I replaced my own tracking ID with GA_MEASUREMENT_ID):

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_MEASUREMENT_ID');
</script>

最近,当我使用Firefox浏览器打开Github Pages网站时,控制台会记录以下警告消息:

Recently, when I open my Github Pages site with Firefox browser, the console logs the following warning messages:

Cookie _ga will be soon rejected because it has the sameSite attribute set to none or an invalid value, without the secure attribute. To know more about the sameSite attribute, read https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite

Cookie _gid will be soon rejected because it has the sameSite attribute set to none or an invalid value, without the secure attribute. To know more about the sameSite attribute, read https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite

如何解决此问题?

推荐答案

cookie_flags字段解决问题.

The gtag.js cookie settings can be customized. Specifically, we can set cookie_flags field to solve the issue.

在跟踪代码中,该行

gtag('config', 'GA_MEASUREMENT_ID');

应更改为

gtag('config', 'GA_MEASUREMENT_ID', { cookie_flags: 'SameSite=None;Secure' });

解决此问题.

因此,整个跟踪代码应如下(不要忘记用自己的跟踪ID替换GA_MEASUREMENT_ID的两次出现):

So the whole tracking code should be the following (Don't forget to replace the two occurrences of GA_MEASUREMENT_ID with your own tracking ID):

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_MEASUREMENT_ID', { cookie_flags: 'SameSite=None;Secure' });
</script>


更新(感谢 @RichDeBourke 用于


UPDATE (Thanks to @RichDeBourke for the comment):

控制台仍然可以记录如下警告消息:

The console can still log a warning message like the following:

Cookie "_ga" has been rejected for invalid domain.

要解决此问题,请 gtag的cookie域. js 需要配置.所以原始答案中提到的那一行

To fix the issue, cookie domain of gtag.js needs to be configured. So the line mentioned in the original answer

gtag('config', 'GA_MEASUREMENT_ID', { cookie_flags: 'SameSite=None;Secure' });

应进一步编辑为

gtag('config', 'GA_MEASUREMENT_ID', {
  cookie_domain: 'YOUR_GITHUB_PAGES_DOMAIN',
  cookie_flags: 'SameSite=None;Secure',
});

然后,整个跟踪代码如下(不要忘记将YOUR_GITHUB_PAGES_DOMAIN替换为您自己的GitHub页面域(通常是<username>.github.io,其中<username>是您的实际GitHub用户名))和GA_MEASUREMENT_ID出现两次并带有您自己的跟踪ID):

Then, the whole tracking code is the following (Don't forget to replace YOUR_GITHUB_PAGES_DOMAIN with your own GitHub pages domain (which is, typically, <username>.github.io where <username> is your actual GitHub user name) and the two occurrences of GA_MEASUREMENT_ID with your own tracking ID):

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_MEASUREMENT_ID', {
    cookie_domain: 'YOUR_GITHUB_PAGES_DOMAIN',
    cookie_flags: 'SameSite=None;Secure',
  });
</script>

这篇关于如何将“安全"设置为“安全". Google Analytics(分析)全球网站代码(gtag.js)使用的Cookie的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆