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

gadget unboxing monitor September 3, 2020 Unboxing of my new LG Ultragear 27GN950-B monitor

I ordered the LG Ultragear 27GN950 from Amazon.de. I compared all the Amazon EU websites and it was the cheapest in Germany. When I ordered it showed that it would deliver in 1-2 months but luckily it came back in stock in a day or two and was dispatched right after. I got the monitor delivered to Ireland within 4-5 days.

May 22, 2019 Access Control in Java

Modifiers fall into two categories:

March 14, 2021 Beginners guide to mine crypto with your computer

Crypto Mining nowadays has been made really easy by apps like NiceHash, CudoMiner, HoneyMiner, etc. Anyone with a computer and some decent CPU or GPU (Graphics Card) can mine and earn quick money. And, because of the rising price of Bitcoin and mining profits, everyone wants to mine nowadays. Ergo, I am putting down a short guide on how to get started with mining.