阻止 Google Analytics 在开发环境、ASP.NET MVC 中收集数据

发布于 2024-10-19 23:11:29 字数 244 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听,心雨的声音 2024-10-26 23:11:29

如果在调试模式下运行,您还可以使用 HttpContext 仅包含 GA 脚本:

@if (!HttpContext.Current.IsDebuggingEnabled)
{
   <script type="text/javascript">
      var _gaq = _gaq || [];
      ...
   </script>
}

You could also use HttpContext to only include the GA script if running in debug mode:

@if (!HttpContext.Current.IsDebuggingEnabled)
{
   <script type="text/javascript">
      var _gaq = _gaq || [];
      ...
   </script>
}
等往事风中吹 2024-10-26 23:11:29

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.

属性 2024-10-26 23:11:29

我将编写一个自定义 HTML 帮助程序,其中包含 Google Analytics 所需的脚本:

@Html.Analytics()

然后,该帮助程序可以使用编译指令并设置 #if DEBUG,它只会发出一个空字符串。顺便说一句, microsoft web helpers package 并查看它是如何实现的:

@Analytics.GetGoogleHtml({your-analytics-webPropertyId-here})

I would write a custom HTML helper which would include the necessary scripts for Google Analytics:

@Html.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:

@Analytics.GetGoogleHtml({your-analytics-webPropertyId-here})
风铃鹿 2024-10-26 23:11:29

您还可以在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文