Implementing Google Analytics in Mvc4/C#?
Asked Answered
R

1

10

I’m 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 pasted 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" which is supposed to let you track your site by using .NET framework. I followed these steps by adding the code to 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 paste it into /Views/Shared/_Layout.cshtml.

Anyone has experience implementing Google Analytics in a MVC4 application?

Thank you!

Roll answered 17/2, 2014 at 15:16 Comment(4)
Have you looked at the Google Analytics widget that comes with NopCommerce?Hylton
Apperently the client had already connected an analytics account. Didn't knoe about this. It's connected to the widget. ThanksRoll
@KristofferAndersson would you please tell me step by step implementation of Google Analytics in MVC4 asp.net?Jagatai
@Jagatai - I'll se if i can find time in short to help you.Roll
E
40

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.

Earring answered 17/2, 2014 at 15:21 Comment(7)
i have to wait for 24 hours?Jagatai
It can take up to 24 hours for the Google Analytics servers to update after you make a change to your tracking codeEarring
Regarding Html.RenderPartial("GoogleAnalytics"), is it better to add that to body or header of HTML page?Removal
Google will probably say put it in the header but generally I always put analytics scripts just before the closing <body> tag so they don't affect the page loading time.Earring
Depends on whether fast-as-possible page loading is more important than tracking. If you put it at the end, quick 2-3 second visits might not register in Google analytics. If people leave your page before it's even fully rendered, that's something you probably want to know about!Gayelord
Google advise that PHP users put the code in an include. this would make it cachable - and therefore reduce page size. Should we not dot the same in ASP.NET? The above will inject all the code into every page (so it wont be cacheable)!?Geof
I don't know why everyone does this. Don't use RenderPartial when you are in the markup. Use Partial and remove the curly braces. RenderPartial is only for when you are in a code block. Same with Action and RenderAction.Queston

© 2022 - 2024 — McMap. All rights reserved.