Andres SK
Andres SK

Reputation: 10974

How to detect if something is blocking Adsense with jQuery?

A lot of people use plugins like Adblock to avoid banners on websites. If you can detect that plugin, you can easily display an image or text of your preference. What's a good approach to achieve that requirement with jQuery?

Upvotes: 1

Views: 977

Answers (1)

Andres SK
Andres SK

Reputation: 10974

If you need to check of a plugin (e.g. Adblock) is blocking your Adsense banners on the client's browser, just use this code. It will check if the banners are empty, and if so, it will place a text (which you can safely replace with a customized image for example).

Suggested html (always inside a div):

<div id="ad1">
    <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-XXXXXXXXXXXXXXXX" data-ad-slot="XXXXXXXXXXXXXXXX"></ins>
    <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</div>

The jQuery script:

$(function(){
    setTimeout(function(){
        var ads_list = $('ins.adsbygoogle');
        if(ads_list){
            ads_list.each(function(){
                if($(this).html().replace(/\s/g, '').length != 0) {
                    return false;
                } else {
                    $(this).parent().text('There should be a banner here... please turn Adblock off!');
                }
            });
        }
    }, 1000);
});

The script runs 1 second after the page has been completely loaded, to give some time to Adsense to actually try to retrieve the requested banners.

Suggestions are welcome!

Upvotes: 2

Related Questions