javascript - 选择退出 Google 分析不起作用
问题描述
我正在使用其他来源的代码为用户提供选择加入和退出谷歌分析的选项。下面的代码没有触发任何选项,它只是加载页面而不是别的。
最好的方法是什么,是否符合 GDRP
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title></title>
<h1> Hello This is Test</h1>
</head>
<body>
<script type="text/javascript">
if(window[disableStr] == true) {
document.getElementById("google_analytics").innerHTML = 'You are opt-out from Google Analytics. <a href="javascript:gaOptin()">Opt-in</a>';}
else {
document.getElementById("google_analytics").innerHTML = 'This page is using <a href="http://analytics.google.com/">Google Analytics</a>. <a href="javascript:gaOptout()">Opt-out</a>';
}
</script>
<script>
var gaProperty = 'UA-XXXXXXX-2';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
// Opt-in function
function gaOptin() {
document.cookie = disableStr + '=false; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
window[disableStr] = false;
location.reload(true);
}
// Opt-in function
function gaOptin() {
document.cookie = disableStr + '=false; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = false;
location.reload(true);
}
</script>
<script>
(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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-2', 'auto');
ga('set', 'anonymizeIp', true);
//ga('set', 'forceSSL', true);
ga('send', 'pageview');
</script>
</body>
</html>
解决方案
我修改了你的代码,你没有 gaOptout 功能。它现在工作:
var gaProperty = 'UA-XXXXXXX-2';
var disableStr = 'ga-disable-' + gaProperty;
if(document.cookie.indexOf(disableStr + '=true') > -1) {
$("#ga").html('You are opt-out from Google Analytics. <a href="javascript:gaOptin()">Opt-in.</a>');
}
else {
$("#ga").html('This page is using <a href="http://analytics.google.com/" target="_blank">Google Analytics</a>. <a href="javascript:gaOptout()">Opt-out.</a>');
}
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
// Opt-in function
function gaOptin() {
document.cookie = disableStr + '=false; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
window[disableStr] = false;
location.reload(true);
}
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
location.reload(true);
}
推荐阅读
- angular - 使用 Kendo 编译 Angular v8 项目时出现 IVY 错误
- jmeter - 如何使用 jmeter 中的步进线程组设置需要在 1 分钟(60 秒)内完成的 1000 个线程?
- jenkins - Jenkinsfile 构建作业参数
- android - Android Studio 中是否有自动调整布局以适应显示尺寸的功能?
- html - 如何在我的html页面中显示word文档
- jsf - 如何使用单选功能禁用 Primefaces 4.0 中的行选择?
- python - SQLAlchemy 'bulk_save_objects' vs 'add_all' 底层逻辑差异?
- angular - 如何以角度处理路由参数?
- javascript - 使用 HTML5 Geolocation 捕获地址
- c# - 无法通过 NuGet 管理器安装/更新任何包