Google AdWords (or any third party) tracking with onclick that really works

Disclaimer: this works for me and my clients today (Sep 2010). If Google or one on the web browsers change something, this may break all tracking, so use this on your own risk.

Below is working code fragments. In the process of making the code work, there was following problems / symptoms:

  • Chrome and Safari refused to follow / ping image src at all;
  • Firefox seemed to ignore / not follow issued redirect 302 to;
  • It all seemed like some sort of Same origin policy;
  • Firefox plug-in Firebug Net Panel showed strange question marks in Persist mode (seemed like request never completes). And on next click it showed – aborted.

This all was caused by fact, that after JavaScript function returned, original – “href” handler somehow canceled image download request. So by adding setTimeout it has nothing to cancel any more.

Your HTML code:
<a href="http://some-lead-page-where-you-cannot-add-or-change-code" onclick="googletrack()">Some lead / sale / page view / other action</a>

Simple example code:
function googletrack(){
setTimeout('(new Image()).src="{yourlabelprovidedbygoogle}&guid=ON&script=0"',100);

Example code with custom label passed as parameter:
function googletrack(label){
setTimeout('(new Image()).src="'+label+'&guid=ON&script=0"',100);

Other fields such as value, etc. can be added as needed.

In my setup I attach onclick handler dynamically using JavaScript that is called from HTML in JavaScript defer mode. Also links in question opens in new window (not sure if it will work if opening in the same window).

Tested on Internet Explorer 8.0.7600.16385, Mozilla Firefox 3.6.8, Google Chrome 6.0.472.53, Opera 10.61, Apple Safari 5.0.1.

Updated Sep 17, 2010.

2 replies on “Google AdWords (or any third party) tracking with onclick that really works”

Comments are closed.