您好我使用以下代码禁用谷歌分析和谷歌标签管理器,但它没有用。
<script>
var gaoop_property = 'UA-xxxxxxxx-x';
var gaoop_disable_str = 'ga-disable-' + gaoop_property;
function gtmOptOut() {
document.cookie = gaoop_disable_str + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[gaoop_disable_str] = true;
window.location.reload();
}
</script>
有谁知道如何解决这个问题?谢谢。
答案 0 :(得分:9)
<script>
var gaoop_property = 'UA-XXXXXXXX-X';
var gaoop_disable_str = 'ga-disable-' + gaoop_property;
console.log(gaoop_disable_str + '=true');
if (document.cookie.indexOf(gaoop_disable_str + '=true') > -1) {
window[gaoop_disable_str] = true;
}
function gtmOptOut() {
delete_cookie("_ga");
delete_cookie("_gat_gtag_UA_43169816_3");
delete_cookie("_gid");
document.cookie = gaoop_disable_str + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[gaoop_disable_str] = true;
window.location.reload();
}
function delete_cookie(name) {
var expires = new Date(0).toUTCString();
var domain = location.hostname.replace(/^www\./i, "");
document.cookie = name + "=; expires=" + expires + "; path=/; domain=." + domain;
}
</script>
添加此脚本并在标题中添加条件
<?php if(empty($_COOKIE['ga-disable-UA-43169816-3'])){ ?>
//google analytics script
<?php } ?>