Hello,

I've found this script over at SeoBook: it tracks clicks made on Adsense link units (that are placed into an iframe), and shows them as goals into Google Analytics.

I've tried the script, and it works fine.

Digging in the comments, anyway, an user pointed out some improvements to be made:

this is just an excerpt, to read the full instructions click here

IE part: here the script just checks for onfocus. Better: trace onfocus (gets focus) and onblur (loses focus) and onmouseover, onmouseout as boolean flags and check for the link via onbeforeunload. This is way more accurate.

In Firefox the iFrame swallows the mouseevent. better: track for mouseover, mouseout event and look in the beforeunload event, if you were in the iframe.

in order to test, just create an own iframe on a page and a document with a link as the content. then replace the 'googlesyndication.com' string in the script
So, I wanted to apply these changes, but I'm stuck with the "testing" part... I've set an alert instead of the tracker and changed the string that is the src of my iframe, but it doesn't seem to work.

There's anyone who could please help me, and post the script with the improvements suggested in the comment?

Here is the script:


Code JavaScript:
function as_click () {
	urchinTracker ('/asclick');
}
 
// incredibly funky onload add-event scripting, for all browsers
 
		 if(typeof window.addEventListener != 'undefined')
		 {
		 	//.. gecko, safari, konqueror and standard
		 	window.addEventListener('load', adsense_init, false);
		 }
		 else if(typeof document.addEventListener != 'undefined')
		 {
		 	//.. opera 7
		 	document.addEventListener('load', adsense_init, false);
		 }
		 else if(typeof window.attachEvent != 'undefined')
		 {
		 	//.. win/ie
		 	window.attachEvent('onload', adsense_init);
		 }
 
		 //** remove this condition to degrade older browsers
		 else
		 {
		 	//.. mac/ie5 and anything else that gets this far
 
		 	//if there's an existing onload function
		 	if(typeof window.onload == 'function')
		 	{
		 		//store it
		 		var existing = onload;
 
		 		//add new onload handler
		 		window.onload = function()
		 		{
		 			//call existing onload function
		 			existing();
 
		 			//call adsense_init onload function
		 			adsense_init();
		 		};
		 	}
		 	else
		 	{
		 		//setup onload function
		 		window.onload = adsense_init;
		 	}
		 }
function adsense_init () {
 
	if (document.all) {  //ie
 
		var el = document.getElementsByTagName("iframe");
 
		for(var i = 0; i < el.length; i++) {
			if(el[i].src.indexOf('googlesyndication.com') > -1) {
 
				el[i].onfocus =  as_click;
			}
		}
 
	} else {   // firefox
 
		window.addEventListener('beforeunload', doPageExit, false);
		window.addEventListener('mousemove', getMouse, true);
 
	}
 
}
 
//for firefox
var px;
var py;
 
function getMouse(e) {
	px=e.pageX;
	py=e.clientY;
}
 
function findY(obj) {
	var y = 0;
	while (obj) {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return(y);
}
 
function findX(obj) {
	var x = 0;
	while (obj) {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return(x);
}
 
function doPageExit(e) {
 
	ad = document.getElementsByTagName("iframe");
	for (i=0; i<ad.length; i++) {
		var adLeft = findX(ad[i]);
		var adTop = findY(ad[i]);
		var inFrameX = (px > (adLeft - 10) && px < (parseInt(adLeft) + parseInt(ad[i].width) + 15));
		var inFrameY = (py > (adTop - 10) && py < (parseInt(adTop) + parseInt(ad[i].height) + 10));
 
		if (inFrameY && inFrameX) {
 
			urchinTracker('/asclick');
 
		}
	}
 
}
 
//end for firefox

Any help is appreciated.

Thank you.