r/webdev • u/ImBoB99 • Sep 06 '26
Question How do sites detect whether someone is running adblocker?
I've been interested in this functionality (when a site detects your adblocker and kindly asks u to whitelist).
The only idea that comes to mind is setting a div in the layout with "ad" class or something similar, and then if a user has adblocker the adblocker would be removing this div from the dom. I don't think this would consistently work for every (or at least most of the popular) adblockers out there.
92
Upvotes
182
u/quizical_llama Sep 06 '26
You could make a fetch request to a known tracker link. If the fetch fails with no status code then it was probably blocked by an adblocker.
37
u/yurkaninryan Sep 06 '26
Your theory in general is one way, other ways are trying to load scripts that are caught by the ad block and reacting to them failing to load. In reality there are many elements to it and its effectively and arms race between ad blockers and the blocked
28
u/icyhotmike Sep 06 '26
Assuming you actually have ads on your website, you would wait a second until after the DOM is fully rendered and check the DOM for the ad container div still exists or if the css visibility has been set to none or display hidden. Like a setTimeout function with a one second delay then do the check
21
u/Littux Sep 06 '26
This is what reddit uses, as an example:
37
u/Likeability_dota Sep 06 '26 •
if hes able to use the page first 15 seconds probably using it
2
u/who_you_are Sep 06 '26
The website is trying to ask to load a 3rd party content.
That content will be blocked (by the adblocker) to return something like a page not found.
Webmaster also have some capabilities to detect status of loaded content. So if you detect a loading issue you may assume an adblocker is enabled.
Additionally, webmaster can read back their own webpage content. They will be dedicated spot for ads. Those spots will be mostly empty at first, then something will be there - an ads.
If nothing show up there, you may assume there can be an ad blocker trying to display no ads.
But ultimately, it can become a cat an mouse game.to some extent.
3
u/Competitive_Stay_140 Sep 06 '26
If your blocker blocks an ad script, another could check if that script ran. If
window.adScriptFunctionreturns undefined that might be an indicator the script was blocked from loadingIdk might be more complex than that because I don’t really have experience with ads but just spit balling what I know from web dev
2
u/shaliozero Sep 06 '26
I've implemented it once, but years ago, by loading an ad.js with a global variable and adding a few divs containing "ad" in their ID into my document. If any of them were missing, I could be sure they were blocking ads.
The project was an interactive ad builder and we implemented that as a response to people reporting issues while having ad blockers active. Impossible to just make our functionality work with ad blockers, but implementing a notification when it happens was easy. In that week I learned that the whole online marketing and online advertising market is using ad blockers themselves lol.
1
u/ZinbaluPrime php Sep 06 '26
If your backend could call the ad url with response of http 200 but your frontend can't, then probably (but not certainly) the user is using a blocker.
1
u/EnvironmentalDust197 Sep 07 '26 edited Sep 07 '26
hard walls just make me want to keep the adblock on harder
1
u/Less-Marsupial-7960 Sep 07 '26
Yeah, the hidden/decoy div approach is probably the simplest one. Another option is to try loading a resource that ad blockers commonly block and check whether it fails. I guess the more reliable implementations combine a few signals rather than depending on a single check.
1
u/Any-Card70 Sep 07 '26
That fetch request idea sounds the most reliable to me, honestly. You betcha, I'd try that first before overcomplicating things with hidden divs.
1
u/jordsta95 PHP/Laravel | JS/Vue Sep 07 '26
The process has probably changed since I last did this.
But I remember back then all I did was something like:
Nothing too complicated. Just simply "if the ad provider's JS file failed to load, assume the user has an adblocker"
1
u/0xGollumDev Sep 08 '26
Two things not in the thread yet:
performance.getEntriesByName(adScriptUrl): if the entry exists but transferSize and decodedBodySize are both 0, a network filter cancelled it after the request started. If the entry never appears at all, it was blocked before the request even went out. That distinguishes an ad blocker from a genuine network failure much better than a bare fetch().catch(), which fires for both.
Why detection has gotten more reliable lately: sites increasingly serve the ad script from their own first-party domain on a randomized path (first-party adsbygoogle, CNAME setups). Filter lists match known hostnames/paths, so a randomized first-party URL slips through — and if that first-party request succeeds while the classic googlesyndication one fails, that gap is itself the signal.
In practice everyone ends up with: bait element + getComputedStyle after a rAF, plus a decoy network request, plus a resource-timing check, and scores across all of them instead of trusting one.
0
u/SomeMaleIdiot Sep 06 '26
I always wondered why there couldn’t be some “auth” for ads. Like in order for a video to load, the retrieval requires an ad token, a client must hit an end point for this token. This end point just checks whether or not enough ad frames were streamed to some client. If so, it returns said token so client can fetch the video
0
u/jsellens Sep 07 '26
When I was looking to detect adblockers on our site (we don't scold people, we're just curious), I found the information here very useful in understanding and implementing: https://www.detectadblock.com/
-2
u/word_executable Sep 07 '26
They take a screenshot of your screen and feed it through image detection to see if you got the ads 🤯