Detect Adsense Blocker

You own a website where the income comes from Adsense. But some thoughtless freeloaders are blocking your adverts from appearing when they view your site either from using a blocker or by using the private mode in their browser.

Don’t they realise this is costing you?

Using a script I found that I have modified, detect when Adsense hasn’t loaded and so display a message to the user where the advert would normally appear. This can be customised to display anything – even a fixed advert from another source.

I will go through the code line-by-line, but if you want the finished product then just skip to the end.

1. Create two files.

The first will contain the javascript – I’ve called mine ‘adsense_block_detector.js’.
The second will contain the message to display (this called also have HTML or other code). I’ve called mine ‘message.html’ and inserted some text that says “Please don’t block my adverts”.

2. Within the javascript file, start off by wanting the script to run when the page loads:


window.onload = function() {

};

3. The script should be delayed by a few seconds (in the example I use 2000ms) to give time for the Adsense to load:


window.onload = function() {
setTimeout(function() {

}, 2000);
};

4. Get the status of the ins.adsbygoogle code that Google supplies to load the advert:


window.onload = function() {
setTimeout(function() {
var ad = document.querySelector("ins.adsbygoogle");

}, 2000);
};

5. If no advert has loaded (i.e. due to a blocker) then load the message.html file created above.


window.onload = function() {
setTimeout(function() {
var ad = document.querySelector("ins.adsbygoogle");
if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
ad.style.cssText = 'display:block !important';
$('ins.adsbygoogle').load('/path/to/file/message.html');
}
}, 2000);
};

Script complete. All you need to do is to call this javascript file and jQuery from the head section of your desired webpage.


....
....
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="/path/to/file/adsense_blocker_detect.js" type="text/javascript"></script>
....
....

Leave a Reply

Your email address will not be published. Required fields are marked *