Skip to content

Instantly share code, notes, and snippets.

@greatestview
Last active March 21, 2016 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greatestview/5d7a1cf67a1e8cf9037a to your computer and use it in GitHub Desktop.
Save greatestview/5d7a1cf67a1e8cf9037a to your computer and use it in GitHub Desktop.
Adblocker Detection

Simple Adblocker Detection

This is a simple vanilla JS adblocker detection script. It is inspired by an article by Christian Heilmann, but there a few differences, mostly for performance reasons:

  • The HTML div element is not created on the fly via JavaScript.
  • The HTML div element is not being removed after script execution.
  • The HTML div element occupies no space, since it is positioned absolutely.
  • A simple Google Analytics call has been added.
/**
* @author Kim-Christian Meyer <kim.meyer@palasthotel.de>
* @see https://www.christianheilmann.com/2015/12/25/detecting-adblock-without-an-extra-http-overhead/
*/
;"use strict";
(function() {
window.addEventListener('load', function() {
var test = document.getElementById('ad12345');
if (test.offsetHeight === 0) {
document.body.classList.add('adblocker');
ga('send', 'event', 'AdBlockerDetection', 'adblocker', null, null);
}
else {
document.body.classList.add('no-adblocker');
ga('send', 'event', 'AdBlockerDetection', 'no-adblocker', null, null);
}
});
})();
<div class="ad adsbox" id="ad12345" style="position: absolute; width: 0; height: 1px; overflow: hidden"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment