4

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:

windows.rwt=function(b,d,e,g,h,f,i,j){
var a=encodeURIComponent||escape,c=b.href.split("#");
b.href=["/url?sa=t\x26source\x3dweb",d?"&oi="+a(d):"",e?"&cad="+a(e):"","&ct=",a(g),"&cd=",a(h),"&url=",a(c[0]).replace(/\+/g,"%2B"),"&ei=7_C2SbqXBMW0-AbU4OWnCw",f?"&usg="+f:"",i,c[1]?"#"+c[1]:""].join("");
b.onmousedown="";
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?

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
  • When I do view source, I can only see: window.rwt=function(a,g,h,n,o,i,c,p,j,d){return true}; I also checked – Harry May 29 '14 at 04:47
  • Since this is quite old, I have no idea how they do it now. – Darryl Hein May 29 '14 at 16:34
  • I have a bit more info, Inspect element > go to tab Consonle > key in rwt.toString() it returns: "function (){_.fj.Ua().ka.apply(_.fj.Ua(),arguments)}" it looks even more cryptic now. – Harry Jun 01 '14 at 10:52

2 Answers2

9

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.

stevemegson
  • 11,843
  • 2
  • 38
  • 43
  • 1
    Another benefit of including the actual URL as opposed to the "/url?" is that a modern browser will/should dns-prefetch so after a user receives the 302 Location: they don't have to wait for the IP to be resolved. – Daniel Rucci Apr 30 '12 at 19:16
1

I think most people expect ads to click through via some sort of tracking system, so I shouldn't worry too much about following this particular javascript implementation - as much as anything that's probably there to ensure that the user sees the correct link in the browsers status bar, that various other interesting bits of info (search terms, position on the result set at the time, who you are, etc) are sent across (without you realising it) and that the links still work if JavaScript is disabled.

Generally, yes directing the user through some tracking page with the ID of the ad they have clicked on, and possibly some additional indication of where they have come from is sensible - that way you aren't relying on other mechanisms (such as JS event handlers) to track clicks on the links, it's certainly the way most ad systems I've used work.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117