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!

Alen Giliana
  • 2,144
  • 3
  • 17
  • 30
WhoAmI
  • 1,188
  • 6
  • 17
  • 47

1 Answers1

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.

Carel
  • 2,063
  • 8
  • 39
  • 65
Stokedout
  • 11,003
  • 5
  • 24
  • 30
  • i have to wait for 24 hours? – Archana Nov 08 '14 at 07:43
  • [It can take up to 24 hours for the Google Analytics servers to update after you make a change to your tracking code](https://support.google.com/analytics/answer/1008083?hl=en) – Stokedout Nov 10 '14 at 08:59
  • 2
    Regarding Html.RenderPartial("GoogleAnalytics"), is it better to add that to body or header of HTML page? – Hajjat Nov 08 '15 at 18:44
  • Google will probably say put it in the header but generally I always put analytics scripts just before the closing tag so they don't affect the page loading time. – Stokedout Nov 08 '15 at 23:04
  • 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! – clayRay Aug 24 '16 at 05:08
  • 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)!? – niico Oct 07 '16 at 02:00
  • 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`. – Jordan Sep 22 '20 at 12:27