Loading...

How to know whether a user is using an adblocker

how-to blog
Ram Patra Published on January 25, 2020
Image placeholder

Adblocker detection is a nice feature to have, given that around 47% of the users now use ad blockers according to GlobalWebIndex.

You can show a friendly message to disable the adblocker to support your website or you can simply not allow the user to use the site until they have allowed ads for your website. People should understand that the content may be free but maintaining the website isn’t. However, the website should show quality ads and a maximum of up to 3-4 ads per page. No pop-ups or all that crap, seriously. That’s why people use adblockers in the first place.

Below is the javascript code which I used to detect adblockers and prevent users from using the website if they have one enabled. This code not only detects adblockers but it also takes care of the Overall Bounce Rate in Google Analytics or tracks the users with adblockers turned on.

Create a file called ads.js with the below code. Adblockers block the javascript files which have ad in their name. So, the below file won’t be loaded when the adblocker is enabled and therefore, the variable (in this file) would be undefined.

isAdBlockActive = false;

File: ads.js

Include script.js in your page where you want to check for adblockers. We check for isAdBlockActive variable in the below code as well as for the element which Google Ads create, i.e, ins.adsbygoogle. If either the variable is undefined or the element isn’t created then the user is blocking ads and we show the modal as well as track on GA. Now, if you aren’t using Google Ads then you can just check whether the variable is defined in script.js.

var AdBlocker = (function () {

    function showModal() {
        $('#modal_ad_blocker').modal(); // show a message to the user when ads are blocked
    }
    
    setInterval(function () {
        // Get the first AdSense ad unit on the page
        var ad = document.querySelector("ins.adsbygoogle");
    
        // If the ads.js or the Google ads are not loaded, show modal and track the event
        if (typeof isAdBlockActive === 'undefined'
            || (ad && ad.innerHTML.replace(/\s/g, "").length === 0)) {
    
            showModal();
    
            if (typeof ga !== 'undefined') {
                // Log an event in Universal Analytics
                // but without affecting overall bounce rate
                ga('send', 'event', 'Adblock', 'Yes', {'nonInteraction': 1});
    
            } else if (typeof _gaq !== 'undefined') {
                // Log a non-interactive event in old Google Analytics
                _gaq.push(['_trackEvent', 'Adblock', 'Yes', undefined, undefined, true]);
            }
        }
    }, 5000); // check every 5 seconds

})();

File: script.js

It is that simple. You can even see how it looks on this website by installing AdBlockPlus Extension for your browser and then reloading this page.

Ram Patra Published on January 25, 2020
Image placeholder

Keep reading

If you liked this article, you may like these as well

June 2, 2019 Overloading in Java

Reusing the same method name in the same class or subclass but with different arguments(and optionally, a different return type) is called Method Overloading in Java.

November 28, 2019 How I got 800 stars on GitHub in a day

Is your project solving a pain point that no other project is solving or is it solving a problem in a way better than others? If the answer is yes then read on.

referral discount wise January 5, 2021 Referral link for Transferwise (now Wise)

If you want to try out Wise (formerly Transferwise), you can use my referral link to register: https://wise.com/invite/u/ramp11 and get one international transfer (up to £500 or equivalent) totally free with no transfer fees.