Ryan Angilly's Blog

Tuesday, January 26, 2010

How Google tracks clicks for backpropagation

I was wondering a few nights ago: How does Google track clicks for their backpropagation algorithms? So I cracked open Firebug, did a search and watched the Console tab while I clicked a link. I was expecting to see a nice little XMLHttpRequest go across the screen… and didn’t.

Upon further inspection, I found that the links in search results all had inline onclick handlers:

A call to clk() in the console tab just returned true, so off to the source for clk() I went.

Cmd+U… Cmd+F… ‘clk’…:

  window.clk=function(d,e,f,j,k,l,m){}

Those sons of bitches. Back to Firebug, and the Net tab. At this point, I noticed a whole bunch of requests that had responded with 204s. The request for these 204’d documents had image/png and image/jpg in the accepts header, and it hits me: they’re pixel tagging. Very clever. But I still want to find clk(), so I see in the Net tab where there was a 19k JS file requested, open it up, do a search, and with a little bit of manual de-minifying:

function(d,e,f,j,k,l,m) {
  if(document.images){
    var a=encodeURIComponent||escape,b=new Image,
        g=window.google.cri++;
    window.google.crm[g]=b;
    b.onerror=b.onload=b.onabort=function() {
      delete window.google.crm[g];
    };

    var c,h,i;

    if(google.v6) {
      c=google.v6.src;
      h=google.v6.complete||google.v6s?2:1;
      i=(new Date).getTime()-google.v6t;i
      delete google.v6;
    }

    b.src=["/url?sa=T","&source="+google.sn,
      e?"&oi="+a(e):"",f?"&cad="+a(f):"","&ct=",
      a(j||"res"),"&cd=",a(k),"&ved=",a(m),
      d?"&url="+a(d.replace(/#.*/,"")).replace(/\+/g,"%2B"):"",
      "&ei=",google.kEI,c?"&v6u="+a(c)+"&v6s="+h+
      "&v6t="+i:"",l].join("")
  }
  return true
};

Jackpot.

When you think about it, this makes WAY more sense than an XHR. It’s cross browser, probably a hell of a lost faster (even though I haven’t taken the time to benchmark it), and just so… Google. This may be old hat to some, but I never though of using pixel tagging dynamically in this way. Something tells me we’ll be using this technique in Engineering Alley at Punchbowl HQ in the next few weeks.