How to Detect if an Adblocker is Being Used with JavaScript
It is common for prosumers and technical individuals, such as programmers, to utilize adblockers. On my website, I estimate that around 20% to 25% of visitors use some form of adblocker. While I have no issues with this, as I support the blog using ads, I wanted to implement a strategy that promotes one of my own products only to those with adblockers enabled. This way, instead of seeing ads from Carbon, the network I use, they will see a link to something I want to promote.
It is important to note that I do not recommend using this technique to display a “disable the adblocker!” message, as it can be annoying for users. If you choose to implement this strategy, consider promoting your own products instead of advertising other companies’ products. It’s just an idea to enhance the user experience.
If you have an adblocker enabled right now, you can see a “Sponsor” right after the title. That is the product I am promoting—it’s something I am currently building and testing by monitoring the number of sign-ups on the waiting list. Without an adblocker, that space is occupied by an ad. I don’t want to overload the page with too many ads, which could negatively impact the experience for the kind people who help keep this blog running.
To achieve this, I use the following JavaScript snippet:
1 | let adBlockEnabled = false; |
Ensure that you place this snippet at the bottom of the page to run it when the DOM is loaded, or wait for the DOMContentLoaded event.
Once you know the value of adblockEnabled
, you can add your own custom ad to the page. Here’s the script I use for that:
1 | if (adblockEnabled) { |
In this script, I load an image and invert it if the user is in dark mode. The image is then transformed into a link that directs to the https://flaviocopes.com website. Finally, I add the image to the page.
Tags: adblocker detection, JavaScript, web development, website monetization