在 localhost 上,我通过将其合并到 if 语句中来禁用我的谷歌分析代码,如下所示:
var s = window.location + "";
if (s.indexOf('localhost') < 0) {
//GA universal analytics tracking snippet
}
但是,在我的整个站点中,有各种事件标签在使用ga('send', 'event', etc...);
- 当我的 GA 片段在 localhost 上被禁用时,这些函数会返回错误 ( Uncaught ReferenceError: ga is not defined
)。
有没有办法禁用这些功能而不将它们全部放入各自的 if 语句中?我在想某种像这样的全局声明可能会起作用,但它不会:
var s = window.location + "";
if (s.indexOf('localhost') > 0) {
ga = function () {};
}
有没有解决这个问题的最佳实践?谢谢!