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

时间:2022-05-26 15:15:02

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.

我有一个ASP.NET MVC(3)应用程序,我已经设置了Google分析。问题是每次我从Visual Studio运行时,Google脚本都会开始收集数据,这当然会扭曲真正的结果。

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?

阻止Google Analytics收集开发环境数据的最佳方法是什么?在我想要分析的每个页面上使用丑陋的#if编译器指令?

What would be a best practice?

什么是最佳做法?

Thanks.

谢谢。

4 个解决方案

#1


6  

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.

Google建议的最佳做法是使用过滤器从GA配置文件中删除数据。这可以通过基于开发机器的IP地址进行过滤,或者通过在用于开发和测试的每个浏览器中设置自定义变量cookie来完成。这种方法意味着您可以删除数据而无需修改主体代码。

#2


6  

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

我会编写一个自定义HTML帮助程序,其中包含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:

然后,这个帮助器可以使用编译指令,并设置#if DEBUG,它只会发出一个空字符串。顺便说一下,在microsoft web helpers包中已经有了这样的帮助器,看看它是如何实现的:

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

#3


6  

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

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

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

#4


2  

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.

您还可以在web.config中为dev环境使用不同的api密钥。这样就不会污染生产数据。我相信密钥也可以留空,然后Google Analytics不会记录任何内容。

#1


6  

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.

Google建议的最佳做法是使用过滤器从GA配置文件中删除数据。这可以通过基于开发机器的IP地址进行过滤,或者通过在用于开发和测试的每个浏览器中设置自定义变量cookie来完成。这种方法意味着您可以删除数据而无需修改主体代码。

#2


6  

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

我会编写一个自定义HTML帮助程序,其中包含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:

然后,这个帮助器可以使用编译指令,并设置#if DEBUG,它只会发出一个空字符串。顺便说一下,在microsoft web helpers包中已经有了这样的帮助器,看看它是如何实现的:

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

#3


6  

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

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

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

#4


2  

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.

您还可以在web.config中为dev环境使用不同的api密钥。这样就不会污染生产数据。我相信密钥也可以留空,然后Google Analytics不会记录任何内容。