无法使用谷歌分析中的 cookie 过滤掉内部流量



这是我在标题中与Google Analytics相关的代码:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-ID"> </script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-ID');
</script>

这是我的JavaScript文件底部包含的代码:

// Send Google Analytics data if the cookie does not exist
if (document.cookie.indexOf("internalTest=") === -1) {
window.ga = window.ga || function () {
(ga.q = ga.q || []).push(arguments)
};
ga.l = +new Date;
ga('create', 'UA-ID', 'auto');
ga('send', 'pageview');
}

CookiehomeTest存在于我的笔记本电脑上,但它仍然在分析中实时显示数据。

我做错了什么?

谢谢。

您正在使用gtag.jsanalytics.js的混合。

默认情况下,函数gtag('config', 'UA-ID');已经发送了网页浏览。取而代之的是,请使用类似此gtag('config', 'GA_TRACKING_ID', { 'send_page_view': false });来防止自动发送网页浏览。

有关使用gtag.js的详细信息,请参阅其文档。

如果您的设置在很大程度上依赖于analytcs.js(Google仍然完全支持(,只需将您的分析摘要切换为类似的东西

<!-- Google Analytics -->
<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-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

进行全新设置时,谷歌建议使用gtag.js.

在您的情况下,最简单的解决方案是将gtag('config', 'UA-ID');放在 if 语句中。

最新更新