koffe14 koffe14 - 1 year ago 123
C# Question

Implementing Google Analytics in Mvc4/C#?

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!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download