Count clicks on external links in ASP.NET

Mar 1, 2007

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)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (12) -

 Brian Kuhn
Brian Kuhn
3/1/2007 7:22:23 PM #

Mads,

I was looking for a quick way to do rough tracking of download links for my site at http://argotic.oppositionallydefiant.com, and this works beautifully. Thanks!

 Dan Atkinson
Dan Atkinson
3/3/2007 8:41:24 AM #

This looks really nice!

Except for one slightly minor issue which appears when I CTRL+click one of the modified links.

Because JavaScript concatenates the href, when someone goes back to that link, the link has now changed. Repeatedly clicking that URL results in a little problem:
ttp://localhost/code/handlers/redirect.ashx?url=http://localhost/code/handlers/redirect.ashx?url=http://localhost/code/handlers/redirect.ashx?url=http://localhost/code/handlers/redirect.ashx?url=http://localhost/code/handlers/redirect.ashx?url=http://localhost/code/handlers/redirect.ashx?url=http://dan-atkinson.com/

Just thought I'd let you know anyhow. I thought about using window.location, butt that overwrites the current tab, which I try to avoid.

Cheers,

Dan

 Claus
Claus
3/3/2007 12:51:44 PM #

Hi Mads,

One could easily mistake this as a cloaking technique.

http://en.wikipedia.org/wiki/Cloaking

Claus

 Simon Scarfe
Simon Scarfe
3/5/2007 6:39:12 PM #

What's with the Javascript? Wouldn't just sticking the direct link in result in catching more outgoing links as well as ridding yourself of the aforementioned back-button issues?

Mads Kristensen
Mads Kristensen
3/5/2007 8:01:03 PM #

I've changed the onclick event, so it only changes the URL once.

oVan
oVan Belgium
12/4/2007 4:53:31 AM #

@Simon: the problem with your idea is that search engines will see your site as having zero outgoing links... and that's definitely not good for SEO.

Chris Blankenship
Chris Blankenship United States
12/12/2007 6:55:26 AM #

I noticed that after I loaded it there was a redirection.xml file in the App_Data folder and not the App_Code folder as mentioned in the original post.  Did I miss something or was that a typo?

bryan migliorisi
bryan migliorisi United States
12/12/2007 8:40:30 PM #

Ok here is my take on the situation:

You are really doing this the wrong way!  As pointed out above, using javascript in your onclick event causes search engines to not see those links.  Additionally, why not just use a redirect url to begin with? \out.aspx?redir=www.whatever.com ?

Personally, my way of handling this would be to use a javascript framework (my personal choice is jQuery) and use the js framework to apply onclick events directly to the dom after the page has rendered.  It keeps javascript out of your HTML which makes it nicer and it eliminates any problems that may arise from having javascript in your url in the first place.

Chris
Chris United States
12/12/2007 9:13:57 PM #

I agree that having links that are not seen by search engines can be a very bad thing.  But keep in mind the original intention of this script:
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.
So would it really be that bad to have links to advertisers that don't show up on the search engines?  As for keeping javascript out of your URL then I would recommend creating a javascript function at the header of your site, and then simply call the javascript in the onclick event - something like...  onclick="tracklink(this)"  - so that it's clean and friendly.

Chris
Chris United States
12/12/2007 9:15:33 PM #

Oh, and it would be great for you to write the code you described... I would find that extremely useful.  8~)

Chris Blankenship
Chris Blankenship United States
12/14/2007 1:11:38 AM #

I decided to write a control that would display the results of the redirector.xml file on my blog page.  Now I can have it on a page in my blog that looks nice and uses my theme.  Feel free to have a look at www.dscoduc.com/.../...ng-your-Counted-Clicks.aspx for more information.

Andr&#233;
André Germany
1/17/2008 6:38:05 AM #

This is really bad for SEO, because Google gives no relevance to links with onClick. In conclusion a well linked sites gives NO ranking-power to its sub-pages when linking with onClick. Anvoid using onClick whenever you can

Pingbacks and trackbacks (3)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.