[英]How can you ignore google tracking when “do not track” flag is set by browser?
What is the best practice for honouring, via Javascript, the DNT flag set in modern browsers? 通过Javascript在现代浏览器中设置DNT标志的最佳做法是什么?
Ultimately, I want to disable the likes of Google Analytics, Facebook pixel and other bespoke tracking codes if it is set. 最终,如果设置了Google Analytics(分析),Facebook Pixel和其他定制跟踪代码,我想将其禁用。
Are there any gotchas to be aware of? 有什么要注意的陷阱吗?
You can read the flag using navigator.doNotTrack
and conditionally load those trackers. 您可以使用
navigator.doNotTrack
读取标志,并有条件地加载这些跟踪器。
For Google Analytics, you could use the following code: 对于Google Analytics(分析),您可以使用以下代码:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script>
if (navigator.doNotTrack !== '1') {
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://www.googletagmanager.com/gtag/js?id=UA-ADD-YOUR-ID';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-ADD-YOUR-ID');
}
</script>
(Also replace ADD-YOUR-ID
with your actual ID.) (还用您的实际ID替换
ADD-YOUR-ID
ID。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.