Prevent Google analytics from gathering data in development environment, ASP.NET MVC

血红的双手。 提交于 2019-12-04 11:44:34

问题


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.


回答1:


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.




回答2:


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})



回答3:


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>
}



回答4:


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.



来源:https://stackoverflow.com/questions/5160005/prevent-google-analytics-from-gathering-data-in-development-environment-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!