May 20, 2014

Track onclick event in google adsense using jquery and iframetracker

Google AdSense is a free and simple way that can use for earning money by display Google ads in your websites. However, if you would like to bind any click events to the Google ads, it is not possible. It is because the Google ads is displaying in iframe which makes your main site javascript cannot bind any click events to it directly due to security reason (since they are in different domain).

Fortunately, there is a trick (work around) to make it possible. The idea is listen blur event in current window and set the focus to window when mouseenter the iframe. Feel strange? No. It is because when the user clicks on the Google Ads, it will pop up a window instead. At that time, it will trigger the blur event in current window. Therefore, we can capture the click event in the blur event of window.

jQuery code example like this :


<script type="text/javascript">
$(window).blur(function(e){
   // Implements in here...
});
$(document).mousemove(function(e){
    if( document.activeElement && document.activeElement.tagName == 'IFRAME' ){
        $(window).focus();
    }
});
</script>

If you feel difficult to implement it yourself, there is a powerful jQuery plugins, "iframeTracker" written by finalclap is for tracking iframe purpose. You utilize that plugins to track the click event in Google ads, for more usage please read the instructions in github.

11 comments:

  1. http://iframetracker/
    Esta página da web não está disponível

    ReplyDelete
  2. iframetracker does not work on mobile devices.

    ReplyDelete
  3. Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete
  4. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete