阻止 Google Analytics 在开发环境、ASP.NET MVC 中收集数据
我有一个 ASP.NET MVC (3) 应用程序,并且我已经设置了 Google 分析。问题是,每次我从 Visual Studio 运行时,Google 脚本都会开始收集数据,这当然会扭曲实际结果。
阻止 Google Analytics 收集开发环境数据的最佳方法是什么除了在我想要分析的每个页面上使用丑陋的 #if
编译器指令 ?
最佳实践是什么?
谢谢。
I have an ASP.NET MVC (3) app and I've set Google analytics up. The problem is that every time I run from Visual Studio the Google script starts gathering data, which of course, skews the real results.
What's the best way to prevent Google Analytics from gathering data on the development environment other that using ugly #if
compiler directives on every page I want analysed?
What would be a best practice?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果在调试模式下运行,您还可以使用 HttpContext 仅包含 GA 脚本:
You could also use HttpContext to only include the GA script if running in debug mode:
Google 建议的最佳做法是使用过滤器从您的 GA 配置文件中删除数据。这可以通过基于开发计算机的 IP 地址进行过滤或通过在用于开发和测试的每个浏览器中设置自定义变量 cookie 来完成。这种方法意味着您可以删除数据而无需修改代码主体。
The best-practice advised by Google is to use a filter to remove the data from your GA profiles. This can either be done by filtering based on the IP address of development machines or by setting a custom variable cookie in each browser being used for development and testing. This approach would mean you could remove the data without needing to modify your main body of code.
我将编写一个自定义 HTML 帮助程序,其中包含 Google Analytics 所需的脚本:
然后,该帮助程序可以使用编译指令并设置
#if DEBUG
,它只会发出一个空字符串。顺便说一句, microsoft web helpers package 并查看它是如何实现的:I would write a custom HTML helper which would include the necessary scripts for Google Analytics:
This helper could then use compilation directives and
#if DEBUG
is set it would simply emit an empty string. And by the way there is already such helper available in the microsoft web helpers package and see how it is implemented:您还可以在 web.config 中为开发环境设置不同的 api 密钥。这样就不会污染生产数据。我相信密钥也可以留空,然后 Google Analytics 不会记录任何内容。
You could also have different api key for dev environment in the web.config. That way it won't pollute the production data. I believe the key can also be left empty and then nothing is logged by Google Analytics.