R – How does Google track search result clicks? Is this the best way


As the question states, I'm trying to figure out how google tracks clicks on search results. When you view the source, you find the following:

 <a href="http://www.yahoo.com/" class=l onmousedown="return rwt(this,'','','res','1','AFQjCNG7Ba-stir4109vlLygPQX7QGf8bg','&amp;sig2=Vx5PLxf04C-yJYZTZfvk8w')"><em>Yahoo</em>!</a>

The function rwt is, which is pretty messy:

var a=encodeURIComponent||escape,c=b.href.split("#");
return true};

So it looks like Google is changing the href of the a tag to /url?... which I'm assuming is where their tracking is. From LiveHeaders in Firefox, it looks like this page is redirecting the browser to the original href of the a tag.

Is this correct and is this the best method of tracking clicks on links on your site, such as ads?

Best Solution

It's actually changing the href of the link rather than the window location. It's setting b.href, and b refers to the link itself. This runs in onmousedown, so when you release the mouse and the click is handled you magically get sent to that new href.

Any click tracking pretty much comes down to sending the user to some equivalent of Google's /url?... script, counting the click, and performing a 302 redirect to the real destination.

This javascript href replacement has the advantage of automatically filtering out any robots that don't run scripts. The downside is that it also filters out any real people that have javascript disabled. If, like Google, you just care which link is most popular with your real human users, this works out quite well. The clicks that you do record should be representative of real human traffic, and you can safely ignore the clicks from non-javascript users because they probably have the same preferences anyway.

Most adverts just link straight to the counting URL with no javascript replacement. This means that you definitely count every real click on the link, but you need to worry about filtering out requests from robots, since they'll now see your counting URL too.

Which you prefer really depends on why you want to track the clicks.

Related Question