Javascript Google Analytics 上是否有设置禁止尚未同意的用户使用 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10668292/
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-08-24 02:26:02  来源:igfitidea点击:

Is there a setting on Google Analytics to suppress use of cookies for users who have not yet given consent

javascriptcookiesgoogle-analytics

提问by JW.

According to EU Article 5(3) of the E-Privacy Directive (a.k.a 'The Cookie Laws'), web sites that target EU users have to gain opt-in consent from users before they set a cookie.

根据欧盟电子隐私指令(又名“Cookie 法”)第 5(3) 条,针对欧盟用户的网站必须在用户设置 cookie 之前获得他们的选择同意。

See ICO Guidance

查看ICO 指南

I am trying to square this with Google Analyticson my web site.

我正在尝试在我的网站上使用Google Analytics来解决这个问题。

I would imagine that Google Analytics (GA) can do a certain level of analytic data gathering without requiringthe use of cookies.

我想谷歌分析 (GA) 可以在不需要使用 cookie 的情况下进行一定程度的分析数据收集。

However, I cannot find any info on this (on the Google sites/settings panels) about how to relay information about the 'state of consent' back to Google during a page request. So, my only option seems to be that I should not embed Google tag code at allif the user has not explicitly given consent. Which seems a bit drastic.

但是,我找不到关于如何在页面请求期间将有关“同意状态”的信息转发回 Google 的任何信息(在 Google 网站/设置面板上)。所以,我唯一的选择似乎是我不应该嵌入谷歌标记代码在所有,如果用户没有明确给出同意。这似乎有点激烈。

Letting my serverside script set a hasConsentedToCookies=FALSEflag in the JavaScript tags would allow me to instruct Google's services to run in a gracefully degraded fashion.

让我的服务器端脚本hasConsentedToCookies=FALSE在 JavaScript 标签中设置一个标志将允许我指示 Google 的服务以优雅降级的方式运行。

Is there a setting on Google Analytics to suppress use of cookies for users that have not yet given consent?

Google Analytics 上是否有设置禁止尚未同意的用户使用 cookie?

If so, where can I find info on this?

如果是这样,我在哪里可以找到这方面的信息?

采纳答案by Yahel

EDIT (2019): The below answer predates GDPR and likely requires revision.

编辑(2019 年):以下答案早于 GDPR,可能需要修改。

Google Analytics has a new set of APIs to assist with compliance with a cookie opt-out. Here's the documentation, and here's their help docs.

Google Analytics 有一组新的 API 来帮助遵守 cookie 选择退出。这是文档,这是他们的帮助文档

There has been some ambiguity as to whether the EU Cookie Regulations (as implemented in member countries) require that passive web analytics tracking requires opt-in mechanisms for compliance. If you're concerned one way or another, consult an attorney. Google is empowering you to make the decision as to how you want to proceed.

关于欧盟 Cookie 法规(在成员国实施)是否要求被动网络分析跟踪需要选择加入机制以实现合规性,一直存在一些模棱两可的问题。如果您以某种方式担心,请咨询律师。Google 授权您决定如何继续。

They'll leave implementation details to you, but, the idea is, once you've determined whether or not to track the user in Google Analytics, if the answer is to not track, you'd set the following property to true before Google Analytics runs:

他们会将实现细节留给您,但是,这个想法是,一旦您确定是否在 Google Analytics 中跟踪用户,如果答案是不跟踪,您可以在 Google 之前将以下属性设置为 true分析运行:

window['ga-disable-UA-XXXXXX-Y'] = true;

Where UA-XXXXXX-Y is your account ID in Google Analytics

其中 UA-XXXXXX-Y 是您在 Google Analytics(分析)中的帐户 ID

As the other posters have noted, Google Analytics relies on cookies. So, you're not able to do any kind of tracking without cookies. If you've determined that someone is not to be cookied for tracking, you'll need to implement something like this:

正如其他发帖人所指出的,谷歌分析依赖于 cookie。因此,如果没有 cookie,您将无法进行任何类型的跟踪。如果您已确定某人不会被 cookie 用于跟踪,则需要实施如下操作:

if(doNotCookie()){
   window['ga-disable-UA-XXXXXX-Y'] = true;
}

Opt In

选择参加

This does require a little bit of jujitsu for when you first load Google Analytics, since this property will need to be set beforeGoogle Analytics runs to prevent tracking from ever happening, which means, for an "opt in to tracking" approach, you'd probably need to implement a mechanism where, on first visit, Google Analytics is automatically disabled in the absence of an opt-in cookie (cookies that determine cookie preferences are explicitly allowed), and then, if an opt-in happens, re-runs Google Analytics. On subsequent pageviews, all would run smoothly.

当您第一次加载 Google Analytics 时,这确实需要一些柔术,因为需要Google Analytics 运行之前设置此属性以防止跟踪发生,这意味着,对于“选择加入跟踪”方法,您d 可能需要实施一种机制,在第一次访问时,如果没有选择加入 cookie(明确允许确定 cookie 偏好的 cookie),谷歌分析会自动禁用,然后,如果发生选择加入,重新运行谷歌分析。在随后的综合浏览量中,一切都会顺利进行。

Could look something like (pseudo-code):

可能看起来像(伪代码):

if( hasOptedOut() || hasNotExpressedCookiePreferenceYet() ){ //functions you've defined elsewhere
     window['ga-disable-UA-XXXXXX-Y'] = true;
}
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
  _gaq.push(['_trackPageview']);


  function onOptIn(){ //have this run when/if they opt-in.
      window['ga-disable-UA-XXXXXX-Y'] = false;
      //...snip...
      //set a cookie to express that the user has opted-in to tracking, for future pageviews
      _gaq.push(['_trackPageview']); // now run the pageview that you 'missed'
   }

Opt Out

选择退出

With this approach, you'd allow the user to opt-out of tracking, which would mean you'd use a cookie to set the ga-disable-UA-XXXXXX-Y'property and a cookie to manage it in the future:

使用这种方法,您将允许用户选择退出跟踪,这意味着您将使用 cookie 来设置ga-disable-UA-XXXXXX-Y'属性,并在将来使用 cookie 来管理它:

if( hasOptedOut() ){ // function you've defined elsewhere 
     window['ga-disable-UA-XXXXXX-Y'] = true;
}

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXX-Y']);
  _gaq.push(['_trackPageview']);

回答by Martin Clarke

As a quick note, the BBC (probably the most popular site in the UK) has taken an interesting approach to complying with cookies - they've displayed a banner to users telling them that cookies are set and provide a couple of links.

快速说明一下,BBC(可能是英国最受欢迎的网站)采取了一种有趣的方法来遵守 cookie——他们向用户展示了一个横幅,告诉他们设置了 cookie 并提供了几个链接。

This one explains what cookies are.This one lets them manage their cookies, but most interestingly of all they supply a link to Google Analytics to allow users to opt-out of GAin its entirety. So, in summary, the BBC have taken the view that they can tell the user what cookies are set and then provide a link to Google to allow the user to opt out of all GA cookies. For me, that's a lot less hassle than you telling GA to opt-out for an address through JS.

这个解释了cookie是什么。这让他们可以管理他们的 cookie,但最有趣的是,他们提供了一个指向 Google Analytics链接,以允许用户完全退出 GA。因此,总而言之,BBC 认为他们可以告诉用户设置了哪些 cookie,然后提供一个到 Google 的链接,以允许用户选择退出所有 GA cookie。对我来说,这比你告诉 GA 通过 JS 选择退出地址要少得多。

回答by severin

You can disable the use of cookies for Google Analytics by specifying the {'storage' : 'none'}option when creating the tracker instance.

您可以通过{'storage' : 'none'}在创建跟踪器实例时指定选项来禁用 Google Analytics 的 cookie 。

See Google's guide on the subjectfor more details.

有关更多详细信息,请参阅有关该主题的 Google 指南

回答by Elmer

I often never ask users to opt out for google analytics, that is because i never set cookies and i never save their ip (and other personal data).

我经常从不要求用户选择退出谷歌分析,那是因为我从不设置 cookie,也从不保存他们的 ip(和其他个人数据)。

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-98765432-1', 'www.example.com', {
  'anonymizeIp': true
  , 'storage': 'none'
  , 'clientId': window.localStorage.getItem('ga_clientId')
});
ga(function(tracker) {
  window.localStorage.setItem('ga_clientId', tracker.get('clientId'));
});
ga('send', 'pageview');

Also check out this code at Convert Google Analytics cookies to Local/Session Storage

还可以在将 Google Analytics cookie 转换为本地/会话存储中查看此代码

This script will not set any cookies, but still track via google analytics. This will actually have the same effect on privacy as using cookies, because google still records the users IP-address.

此脚本不会设置任何 cookie,但仍会通过谷歌分析进行跟踪。这实际上与使用 cookie 对隐私的影响相同,因为谷歌仍然记录用户的 IP 地址。

That is where the anonymizeIp switch comes in. This tells google to only save an anonymized version of the IP-address. An anonymized IP-address is not considered personal data, so the users privacy will be respected.

这就是 anonymizeIp 开关的用武之地。这告诉 google 只保存 IP 地址的匿名版本。匿名 IP 地址不被视为个人数据,因此将尊重用户隐私。

AFAIK cookie law is all about privacy and does allow website to track their usage. I am not a lawyer or anything but in my opinion this script complies to the EU cookie law.

AFAIK cookie 法完全是关于隐私的,并允许网站跟踪其使用情况。我不是律师或其他任何人,但在我看来,这个脚本符合欧盟 cookie 法。

Check out this plunk to see it in action: http://plnkr.co/MwH6xwGK00u3CFOTzepK

看看这个 plunk 看看它在行动:http: //plnkr.co/MwH6xwGK00u3CFOTzepK

回答by noname

You can disable google analytics cookies by adding this code at the top of google analytics code (before line: var _gaq = _gaq || [];):

您可以通过在 google 分析代码顶部添加此代码来禁用 google 分析 cookie(在行之前:var _gaq = _gaq || [];):

ga('create', 'UA-XXXXXX-XX', {'storage': 'none'});
ga(function(tracker) {
  var clientId = tracker.get('clientId');
});

However some features of google analytics (for example real time stats) do not work properly after this modification. More about google analytics cookies: https://developers.google.com/analytics/devguides/collection/analyticsjs/domains?hl=en#disableCookies

然而,谷歌分析的某些功能(例如实时统计)在此修改后无法正常工作。有关谷歌分析 cookie 的更多信息:https: //developers.google.com/analytics/devguides/collection/analyticsjs/domains?hl=en#disableCookies

回答by Alex Taylor

EDIT: There isa Google Analytics setting for this with the Asynchronous GA snippet.

编辑:有与异步GA代码段一个谷歌Analytics(分析)设置此。

There isn't a Google Analytics setting for this, as you're suggesting, you would need to conditionally exclude the script for those that have not consented if you want to use the Google Analytics Javascript tracking script.

没有针对此的 Google Analytics 设置,正如您所建议的,如果您想使用 Google Analytics Javascript 跟踪脚本,则需要有条件地排除那些未同意的脚本。

There are some solutions out there already that can be helpful instead of rolling your own. Javascript: http://cookies.dev.wolf-software.com/demo/index.htm

已经有一些解决方案可以提供帮助,而不是推出您自己的解决方案。Javascript:http: //cookies.dev.wolf-software.com/demo/index.htm

Here is a solution that allows using Google Analytics basic features without cookies, by doing the tracking server side, this example is in PHP: http://techpad.co.uk/content.php?sid=205

这是一个允许在没有 cookie 的情况下使用 Google Analytics 基本功能的解决方案,通过跟踪服务器端,这个例子是在 PHP 中:http: //techpad.co.uk/content.php?sid=205

回答by Chris Disley

Common way to handle this so far is the method used by wolf-software's jquery plugin whereby it prevents the script from running until the user opts in. The ICO updated their guidelines last week, however, to say that it is acceptable to rely on 'implied consent' of the sort used on the BBC site. While I don't really think that's within the spirit of the law, it's what's deemed acceptable by those enforcing it. Given that most of the EU has yet to implement the directive, I'd say it's highly likely they'll follow the UK's lead.

到目前为止,处理这个问题的常用方法是 wolf-software 的 jquery 插件使用的方法,它阻止脚本运行,直到用户选择加入。 然而,ICO 上周更新了他们的指导方针,说依赖于 ' BBC 网站上使用的那种默示同意。虽然我真的不认为这符合法律精神,但执法人员认为这是可以接受的。鉴于欧盟的大部分地区尚未实施该指令,我认为他们很可能会效仿英国的做法。

There's an interesting article about the UK updates here:

这里有一篇关于英国更新的有趣文章:

http://www.redant.com/articles/eu-cookie-law-update-ico-adopts-softly-softly-approach/

http://www.redant.com/articles/eu-cookie-law-update-ico-adopts-softly-softly-approach/

回答by Matt Clegg

For a less intrusive UX solution you can set implied consent for google analytical cookies by placing a link to: cookiestatement.eu (no javascript, no popups, no ads)

对于侵入性较小的 UX 解决方案,您可以通过放置以下链接来为 google 分析 cookie 设置默示同意:cookiestatement.eu(无 javascript,无弹出窗口,无广告)

回答by Pezmo

Sorry for being late to answer but I was looking for the same thing recently until I found out a way myself. It may not be the right way to do it but it works. (only works on site in question does not opt-out of GA completely). I have tested for a few days to make sure.

抱歉迟到了,但我最近一直在寻找同样的东西,直到我自己找到了一种方法。这可能不是正确的方法,但它有效。(仅在有问题的网站上工作不会完全退出 GA)。我已经测试了几天以确保。

The way I have managed to do it is using a PHP cookie. First start off with adding the analyticstracking.php include...

我设法做到的方式是使用 PHP cookie。首先开始添加analyticstracking.php 包括...

<?php include_once('analyticstracking.php'); ?>

and in analyticstracking.php add the following...

并在 analyticstracking.php 中添加以下内容...

<?php
if($_COOKIE['consent_cookie']=="Y"){
?>

<script type="text/javascript">
var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-********-*']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript';     ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' :      'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];      s.parentNode.insertBefore(ga, s);
  })();
</script>

<?php   
}
else{
//do nothing
}
?>

Before the user has consented to cookies Google Analytics won't work and once they have, the 'consent_cookie' will be saved and will allow GA to work but if the 'google' cookie is destroyed it will stop GA from working (Obviously).

在用户同意 cookie 之前,Google Analytics 将无法工作,一旦他们同意,'consent_cookie' 将被保存并允许 GA 工作,但如果 'google' cookie 被销毁,它将停止 GA 工作(显然)。

Like I said it may not be the right way but I have tried and tested and it does. Hope this helps somebody.

就像我说的那样,这可能不是正确的方法,但我已经尝试并测试过,确实如此。希望这可以帮助某人。

回答by Scriptor

GA does not work without cookies, it needs it to 'identify s' the visitor if he/she visited your site before. So there is no setting in GA for this, GA just doesn't records the visitor if it cant create a cookie.

GA 在没有 cookie 的情况下无法工作,如果他/她之前访问过您的网站,它需要它来“识别”访问者。因此,GA 中没有对此进行设置,如果无法创建 cookie,GA 就不会记录访问者。

If the user is from the EU and has not opt-in then you should exclude the google-analytics script I think.

如果用户来自欧盟并且没有选择加入,那么我认为您应该排除 google-analytics 脚本。