admin管理员组文章数量:1026989
On localhost, I disable my google analytics code by incorporating it into an if statement like so:
var s = window.location + "";
if (s.indexOf('localhost') < 0) {
//GA universal analytics tracking snippet
}
However, throughout my site there are various event tags using ga('send', 'event', etc...);
- when my GA snippet is disabled on localhost these functions return errors (Uncaught ReferenceError: ga is not defined
).
Is there a way to disable these functions without putting them all into their own individual if statements? I was thinking some kind of global statement like this might work, but it doesn't:
var s = window.location + "";
if (s.indexOf('localhost') > 0) {
ga = function () {};
}
Is there a good best practice for solving this? Thanks!
On localhost, I disable my google analytics code by incorporating it into an if statement like so:
var s = window.location + "";
if (s.indexOf('localhost') < 0) {
//GA universal analytics tracking snippet
}
However, throughout my site there are various event tags using ga('send', 'event', etc...);
- when my GA snippet is disabled on localhost these functions return errors (Uncaught ReferenceError: ga is not defined
).
Is there a way to disable these functions without putting them all into their own individual if statements? I was thinking some kind of global statement like this might work, but it doesn't:
var s = window.location + "";
if (s.indexOf('localhost') > 0) {
ga = function () {};
}
Is there a good best practice for solving this? Thanks!
Share Improve this question edited Jan 6, 2014 at 13:23 Denys Séguret 383k90 gold badges811 silver badges777 bronze badges asked Dec 24, 2013 at 20:18 YPCrumbleYPCrumble 28.8k25 gold badges112 silver badges175 bronze badges1 Answer
Reset to default 8It would be simpler to do this :
var ga = ga || (function(){});
If ga
is defined, this does nothing. If it's undefined
, it sets its value to a no-op function, preventing the error.
But I don't think it's a good idea to disable the script when you develop : it makes one more reason to have an unexpected bug in production. The best practice here would be, in my opinion, to add a filter in Google Analytics. See Exclude internal traffic.
On localhost, I disable my google analytics code by incorporating it into an if statement like so:
var s = window.location + "";
if (s.indexOf('localhost') < 0) {
//GA universal analytics tracking snippet
}
However, throughout my site there are various event tags using ga('send', 'event', etc...);
- when my GA snippet is disabled on localhost these functions return errors (Uncaught ReferenceError: ga is not defined
).
Is there a way to disable these functions without putting them all into their own individual if statements? I was thinking some kind of global statement like this might work, but it doesn't:
var s = window.location + "";
if (s.indexOf('localhost') > 0) {
ga = function () {};
}
Is there a good best practice for solving this? Thanks!
On localhost, I disable my google analytics code by incorporating it into an if statement like so:
var s = window.location + "";
if (s.indexOf('localhost') < 0) {
//GA universal analytics tracking snippet
}
However, throughout my site there are various event tags using ga('send', 'event', etc...);
- when my GA snippet is disabled on localhost these functions return errors (Uncaught ReferenceError: ga is not defined
).
Is there a way to disable these functions without putting them all into their own individual if statements? I was thinking some kind of global statement like this might work, but it doesn't:
var s = window.location + "";
if (s.indexOf('localhost') > 0) {
ga = function () {};
}
Is there a good best practice for solving this? Thanks!
Share Improve this question edited Jan 6, 2014 at 13:23 Denys Séguret 383k90 gold badges811 silver badges777 bronze badges asked Dec 24, 2013 at 20:18 YPCrumbleYPCrumble 28.8k25 gold badges112 silver badges175 bronze badges1 Answer
Reset to default 8It would be simpler to do this :
var ga = ga || (function(){});
If ga
is defined, this does nothing. If it's undefined
, it sets its value to a no-op function, preventing the error.
But I don't think it's a good idea to disable the script when you develop : it makes one more reason to have an unexpected bug in production. The best practice here would be, in my opinion, to add a filter in Google Analytics. See Exclude internal traffic.
本文标签:
版权声明:本文标题:javascript - How can I ignore ga() event tracking code throughout my site when I remove google analytics in localhost? - Stack O 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745663292a2162022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论