Detect google ads click
If I recall correctly - it's against Google Ads rules for you to track ad clicks, because it leads to providing incentives for clicking the ads. (such as disabling them). I realize you're trying to fix the illegal clicks issue, but you're in fact digging your own grave.
But to answer your question with purely web-dev interest - you can detect the mouse X Y position (+ page scroll offset) in window.onbeforeunload
and quickly ping your server. Usually this is done by creating a new Image();
with the source being a php file.
This is accurate for all browsers, regardless of iframe usage.
Good Luck!
Google ads are iframes and Javascript does not have access to the contents or adding events to it if it's on a different domain then the parent site (see the law of iframes here).
You can, however, place a clear div (visibility: hidden
, not display: none
) over the iframe and, in a way, intercept the clicks to it. The only issue with this is that you either intercept the click or you don't. So when the user clicks the first time you can run your logic on whether to allow it or not, and if you want to allow it, display: none
your div and prompt the user to click again.
This is pretty much the only way you can do it.
You can use iframetracker plugin.
<script src="jquery.min.js"></script>
<script src="jquery.iframetracker.js"></script>
$('iframe').iframeTracker({
blurCallback: function(){
// Do something when clicked on ad
}
});
for more info and demo check here.