简体   繁体   English

如何在localhost上禁用Google Analytics

[英]how to disable google analytics on localhost

I use this for google analytics, 我用它来进行谷歌分析,

<noscript>
<iframe src="//www.googletagmanager.com/ns.html?id=GTM-KCQGLT" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

and this 和这个

<script>(function(w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
    'gtm.start': new Date().getTime(),
    event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
    j = d.createElement(s),
    dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src =
    '//www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);})(window, document, 'script', 'dataLayer', 'GTM-KCQGLT');</script>

Can I disable this script when I run this code in localhost? 我在localhost中运行此代码时可以禁用此脚本吗?

Use below code. 使用以下代码。

<script>
var host = window.location.hostname;
if(host != "localhost")
{
    // your google analytic code here
}
</script>

As described here , this is the correct way to prevent further events being sent to google analytics, even after ga(...) has been initialized: 如上所述这里 ,这是为了防止被送到其他事件到Google Analytics正确的方式,即使在ga(...)已经被初始化:

if (admin || localhost) { // disable GA:
  window['ga-disable-UA-XXXXX-Y'] = true; // enter your tracking ID
}

Now any further calls to ga(...) will have no effect. 现在任何进一步调用ga(...)都没有效果。

Quoting the docs: 引用文档:

The analytics.js library includes a window property that, when set to true, disables analytics.js from sending data to Google Analytics. analytics.js库包含一个窗口属性,当设置为true时,禁用analytics.js将数据发送到Google Analytics。 When Google Analytics attempts to set a cookie or send data back to the Google Analytics servers, it will check whether this property is set to true. 当Google Analytics尝试设置Cookie或将数据发送回Google Analytics服务器时,它会检查此属性是否设置为true。 If it is, no action will be taken. 如果是,则不采取任何行动。

To disable tracking, set the following window property to true: 要禁用跟踪,请将以下窗口属性设置为true:

window['ga-disable-UA-XXXXX-Y'] = true;

Where the value UA-XXXXX-Y corresponds to the property ID on which you would like to disable tracking. 其中值UA-XXXXX-Y对应于您要禁用跟踪的属性ID。

转到“Analytics设置”,编辑您的站点,然后选择“+添加过滤器”以定义排除IP地址的过滤器。

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

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