On a small test website I’m building in my spare time, I wanted a way to count the number of clicks to the outgoing links from that site. Imaging hosting banners or other affiliate links where you get paid per click, it would be nice to know how many have clicked on them instead of relying on the advertiser’s own statistics. There are third-party products that can do it, but I would like to keep that data myself and it would be fun to build at the same time.

I came up with a very simple solution that uses a generic handler called redirect.ashx. The handler registers the clicks in an XML file and is also able to print out the stats in a nice looking table. It works by linking to redirect.ashx?url=http://example.com instead of http://example.com.

To get the stats from the handler, call it with the stats parameter redirect.ashx?stats=true. That prints out a table with the statistics from each redirected URL.

Crawlers and spiders

To avoid that the clicks made by crawlers and spiders, you can use this simple little trick that filters them out.

<a href="http://example.com" onclick="if(this.href.indexOf('redirect.ashx')==-1)this.href='redirect.ashx?url='+this.href">Click here</a>

By using the onclick event on the link, all clients that don’t support JavaScript will just follow the original URL and never hits the redirect.ashx file.

Implementation

Download the redirect.ashx handler below and put it somewhere in your web project. If your website doesn’t already have an App_Code folder in the root, then add it. That’s all you need to do. The handler automatically adds an XML file in the App_Code folder the first time it is hit and it automatically creates an XML node for every new URL it redirects.

Download

redirect.zip (1,48 KB)

Finally I had the chance to install Vista on my work PC - something I've wanted to do since November when it became available on MSDN. Now the installation is over and it went incredibly smooth. It installed all my hardware except my onboard audio controller. The installation took 30 minutes without any problems at all. After the first start of Vista it automatically downloaded an installed the driver for my audio controller. Excellent!

I didn't format my C drive before installing, but went for the easy solution. It's the first time I've ever done that on my own PC. The cool thing about it is that the Vista installation does not delete the folders on the C drive, but leaves them alone. Except the windows folder, that is renamed to windows.old so you still have access to all your documents etc. I'll take what I need and then delete it.

Then Office Ultimate 2007 was intalled in 15 minutes without any problems and the same with Visual Studio 2005. Visual Studio 2005 needs Service Pack 1 and another small update to run on Vista, and right now the service pack is installing. I'm excited to see how the new IIS 7 works with our existing web applications and how Visual Studio debugs through it.

Source Safe is also a joker I think. Even on XP it was extremely unstable and at best annoying, but it worked.

So far, there has been no problems at all. I'll hope it continues.

Update: some hours later

It took some hours to install SQL Server 2005 and download Service Pack 2 that you need to run it on Vista. Source Safe intallation was fast and error less and it hooked right into Visual Studio 2005 as expected. When it came to running our web application Headlight on the local IIS 7 the trouble started. ASP.NET is not installed by default and after I installed it, the mappings where wrong. Luckily there were help to get right here and in no time, everything worked great.

All in all it took me 6 hours to install the following, including the download time for the service packs:

  • Windows Vista Business x64 Edition
  • Visual Studio 2005 + SP1 + SP1 update
  • SQL Server 2005 Management Studio x64 + SP2
  • Office Ultimate 2007
  • Visual Source Safe 2005
  • Getting my web applications running on IIS 7

After playing around with Vista I feel it was a very good idea to taking the time to install it. It looks and feels great.