在 Angular 中禁用本地主机上的 Google 分析
Disable Google Analytics on localhost in Angular
我想阻止 Google Analytics 在本地主机上开发我的 Angular 11 应用程序时记录事件。我该怎么做?
到目前为止,我试过这个:
if(window.location.hostname == 'localhost') {
window['ga-disable-GOOGLE_TAG_HERE'] = true;
} else {
window['ga-disable-GOOGLE_TAG_HERE'] = false;
}
这给了我错误:
Element implicitly has an 'any' type because index expression is not of type 'number'
提前致谢!
修复该特定错误消息的一种方法是像这样修改您的代码:
if(window.location.hostname == 'localhost') {
window[<any>'ga-disable-GOOGLE_TAG_HERE'] = true;
} else {
window[<any>'ga-disable-GOOGLE_TAG_HERE'] = false;
}
或者像这样:
if(window.location.hostname == 'localhost') {
window as { [key: string]: any })['ga-disable-GOOGLE_TAG_HERE'] = true;
} else {
window as { [key: string]: any })['ga-disable-GOOGLE_TAG_HERE'] = false;
}
我建议您在开发中也包括 GA 日志记录。如果开发中的GA环境有问题可以直接发现。
诀窍是为开发、测试、验收和上线创建单独的视图。
- Add a new view in GA。请注意,过滤器仅适用于 Universal Analytics Properties。过滤器无法应用于 Google Analytics 4 媒体资源。
- GA 4 works with Data Streams, and Data Filters.
我想阻止 Google Analytics 在本地主机上开发我的 Angular 11 应用程序时记录事件。我该怎么做?
到目前为止,我试过这个:
if(window.location.hostname == 'localhost') {
window['ga-disable-GOOGLE_TAG_HERE'] = true;
} else {
window['ga-disable-GOOGLE_TAG_HERE'] = false;
}
这给了我错误:
Element implicitly has an 'any' type because index expression is not of type 'number'
提前致谢!
修复该特定错误消息的一种方法是像这样修改您的代码:
if(window.location.hostname == 'localhost') {
window[<any>'ga-disable-GOOGLE_TAG_HERE'] = true;
} else {
window[<any>'ga-disable-GOOGLE_TAG_HERE'] = false;
}
或者像这样:
if(window.location.hostname == 'localhost') {
window as { [key: string]: any })['ga-disable-GOOGLE_TAG_HERE'] = true;
} else {
window as { [key: string]: any })['ga-disable-GOOGLE_TAG_HERE'] = false;
}
我建议您在开发中也包括 GA 日志记录。如果开发中的GA环境有问题可以直接发现。
诀窍是为开发、测试、验收和上线创建单独的视图。
- Add a new view in GA。请注意,过滤器仅适用于 Universal Analytics Properties。过滤器无法应用于 Google Analytics 4 媒体资源。
- GA 4 works with Data Streams, and Data Filters.