简体   繁体   English

如何删除谷歌分析cookie?

[英]How to remove google analytics cookies?

With the new Cookie law i'm trying to find a way i can remove all cookie from my site with a PHP script. 使用新的Cookie法则我试图找到一种方法,我可以使用PHP脚本从我的网站删除所有cookie。 I have got a script which works but it doesn't remove google analytics cookie. 我有一个有效的脚本,但它不会删除谷歌分析cookie。

How can I remove google analytics cookie? 如何删除Google Analytics分析Cookie?

foreach($_COOKIE as $key => $value) {
      setcookie($key, '', time()-1000);
      setcookie($key, '', time()-1000, '/');
  }

您需要以第5个参数传递域名...

 setcookie($key, '', time()-1000, '/', '.domain.com');

usually firefox shows you whether the cookie was set under www.yourdomain.com or domain.com. 通常firefox会向您显示cookie是否在www.yourdomain.com或domain.com下设置。 In my case the google analytics cookies had been set for domain.com. 就我而言,已经为domain.com设置了谷歌分析cookie。 This code did the job, deleting all 3 Google-related cookies: 此代码完成了这项工作,删除了所有3个与Google相关的Cookie:

foreach ( $_COOKIE as $key => $value ){
if($_POST[$key]==1){
unset($_COOKIE[$key]);
if($key=='language'  || $key=='currency'){
setcookie($key, null, -1, '/', 'www.mydomain.com');
}else if($key=='_ga' || $key=='_gid' || $key == '_gat_gtag_UA_whatever_1' ){
setcookie($key, null, -1, '/', '.mydomain.com');
}else{
foreach ( $_COOKIE as $key => $value ){
if($_POST[$key]==1){
unset($_COOKIE[$key]);
if($key=='language'  || $key=='currency'){
        setcookie($key, null, -1, '/', 'www.mydomain.com');
}else if($key=='_ga' || $key=='_gid' || $key == '_gat_gtag_UA_whatever_1' ){
        setcookie($key, null, -1, '/', '.mydomain.com');
}else{
        setcookie($key, null, -1, '/');
}
$n++;
}
}

I let this run as a form action where I list all cookies stored currently in a client's browser and let him select which one he wants to delete. 我让它作为一个表单操作运行,我列出了当前存储在客户端浏览器中的所有cookie,让他选择要删除的那个。 Those selected come in as $_POST[$key]==1 and are then handled by the script. 选中的那些作为$ _POST [$ key] == 1进入,然后由脚本处理。 The not-selected stay alive. 未被选中的活着。 This is for an online shop under OpenCart. 这是OpenCart下的在线商店。 Needless to say, replace 'mydomain.com' by your's and 'whatever' in the tracking cookie by your google account numbers. 不用说,用您的谷歌帐号替换跟踪cookie中的'和'任何'的'mydomain.com'。

You cant remove a cookie on a clients browser that belongs to a different domain .google, but you could give the user a choice in adding the analytics code. 无法删除属于其他域.google的客户端浏览器上的Cookie,但您可以选择添加分析代码。

First step would be use PHP to check for the dnt header [example function] , many browsers already support this + AVG has this as a browser plugin, if its there then default dont add the js code. 第一步是使用PHP来检查dnt[示例函数] ,很多浏览器已经支持这个+ AVG将此作为浏览器插件,如果它那里默认不添加js代码。 Or if the dnt header is not set then you can prompt the user about your tracking/analytic cookies, then store there choice in a cookie or a session. 或者,如果未设置dnt标头,则可以提示用户有关您的跟踪/分析cookie,然后将选择存储在cookie或会话中。

foreach($_COOKIE as $key => $value) {    
    setcookie($key, '', time()-1000, '/', '.domain.com');
}

The above example doesn't work for me - all other cookies are removed leaving only the GA ones. 上面的示例对我不起作用 - 所有其他cookie都被删除,只留下GA。 Anyone know why this might be or a better method? 任何人都知道为什么这可能是一个更好的方法?

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

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