Always Hide Your Webstats Code On Your Dev Server

While Google Analytics may have been the bees knees when it comes to web stats in the past, it seems to have taken an almighty tumble from the top spot in recent years. The interface has gone from being one of the cleanest, easiest to use, to one of the most confusing out there.

I decided it was time to consider moving away from Google Analytics, so recently I have been playing around with a few different web stat packages, running multiple at once on the same page. In doing so, I began to notice that some packages were reporting a higher number of views than others.

When I started to dig closer to see what was causing this, I discovered that some of the packages were showing a large number of hits to pages `localhost:page_name`.

Yep, it seems that not all of the packages are clever enough to filter out page views from your development server.

To resolve the issue – I found a nice little hack that will only display the tracking code on the live server.

The ASP.NET property `Request.Url.Host` will reveal the URL by which the site was accessed – by simply checking this value you can tell whether the site is live, or running on a dev server. You can then hide the tracking code accordingly.

@if(Request.Url.Host != "localhost")
// show tracking code - site is live

Read More