Skip to content

Instantly share code, notes, and snippets.

@mikeflynn
Created July 20, 2011 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeflynn/1094737 to your computer and use it in GitHub Desktop.
Save mikeflynn/1094737 to your computer and use it in GitHub Desktop.
Tracking Clicks on iFrame Ads
// Limitations:
// 1. We won't know where they are headed.
// 2. If the ads open in a new window, it doesn't trigger the unload event.
// 3. If there are multiple ad units in the iframe, we won't know which one was clicked.
function iframe_click_tracking(selector) {
jQuery(selector).bind('mouseover', function() {
// If they are over the ad and click, the unload will trigger to log the click.
jQuery(window).unload(function(){ // Event: onbeforeunload
jQuery.ajax({url:'http://local.logger.com/logger/?log=mobile&message=realclick', async: false});
});
// If they leave the ad, then they aren't going to click. Kill the unload event.
jQuery(this).mouseout(function(){ // Event: onmouseout
jQuery(window).unbind('unload');
});
// So if the ads open in a new window and the unload isn't called, kill the unbind after a while.
setTimeout(function(){
jQuery(window).unbind('unload');
}, 2000);
});
}
iframe_click_tracking('div.ads iframe');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment