Rich-text Reply

How to track AdSense clicks

hv 01-07-16
Accepted Solution

How to track AdSense clicks

Hey there,

 

I want to set as a Goal and track the clicks on AdSense ads on my page. Is that possible?

 

Regards

Harry

hv
Level 2

robertchan 01-07-16
 

Re: How to track AdSense clicks

Add a class to your ad element (edit element > edit classes > click the plus button and create a class, maybe you could call it optAd). Click on goals and create a new goal, on "What to Track," use "Clicks." Now click on advanced within the same area you changed your goal to clicks and add the class you created to the Selector section (e.g. .optAd). Give that a name, save, and you're set.
Robert Chan

hv 01-08-16
 

Re: How to track AdSense clicks

Thanks. But I think this doesn't work. The AdSense ads are loaded within an iFrame, so a click in there doesn't trigger the EventListener on the Div container around it.

 

 

Something I found working is this:

 

 

focus();
var listener = addEventListener('blur', function() {
   if(document.activeElement === document.getElementById('iFrameID')) {
       // Push TrackEvent
   }
   removeEventListener(listener);
});

So when the page looses focus and the now active element is the iFrame the tracker gets executed.

 

But maybe there's a better solution?

hv
Level 2
hv 01-08-16
 

Re: How to track AdSense clicks

Solution above didn't work properly in all browsers.

 

This works:

 

    var isOverAds = false,
    windowLostBlur = function () {
        if (isOverAds) {
           // Push Tracking Event
        }
    };
  
    $(window).focus();
    $(AdSenseContainerID).mouseenter(function(){
        isOverAds = true;
    });
  
    $(AdSenseContainerID).mouseleave(function(){
         isOverAds = false;
    });
  
    $(window).blur(function () {
        windowLostBlur();
    });
 
hv
Level 2