Implementing Google Analytics in Mvc4/C#?

China☆狼群 提交于 2019-11-27 15:11:28

问题


Im trying to implement Google Analytics in my mvc website. First i tried creating a GA account, unfortunately i'm developing locally on localhost which isn't a valid site URL but i found a fix that will hopefully work here http://www.objectpartners.com/2011/05/26/setting-up-google-analytics-on-localhost/#comment-5960.

Then i copied the generated JS tracking code and paisted it to a view. However i found this article (http://analyticsimpact.com/2011/01/20/google-analytics-on-intranets-and-development-servers-fqdn/) about using NuGet package "GoogleAnalyticsTracker" whitch is supposed to let you track your site by using .NET framework. I followed these steps by adding the codeto a controller but the nothing is shown in the view.

I guess one solution would be creating a new GA account, copy the JS tracking code and paiste it into /Views/Shared/_Layout.cshtml.

Has annyone anny experiance implementing Google Analytics in a MVC4 application?

Thank you!


回答1:


It's really as simple as:

  1. Create a partial view named GoogleAnalytics
  2. Copy & Paste the Analytics tracking JavaScript code from Google
  3. Use @{ Html.RenderPartial("GoogleAnalytics"); } in a template which is used by all pages
  4. Publish the site
  5. Wait 24 hours for statistics to appear

This is my organised approach however you can put it in any location as long as the code is visible on every page you want to track.



来源:https://stackoverflow.com/questions/21832711/implementing-google-analytics-in-mvc4-c

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