Skip to content

Instantly share code, notes, and snippets.

@mokoshalb
Created December 26, 2017 23:51
Show Gist options
  • Save mokoshalb/7849c6f59004462253386799d34294b3 to your computer and use it in GitHub Desktop.
Save mokoshalb/7849c6f59004462253386799d34294b3 to your computer and use it in GitHub Desktop.
AdBlock detector for Google AdSense. Detects if a user is using AdBlock or not and sends the tracking data to Google Analytics.
function AdblockDetector(){
}
AdblockDetector.prototype.run = function() {
var ad = document.querySelector("ins.adsbygoogle");
if(ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
//Adblock detected
if(typeof ga !== 'undefined') {
ga('send', 'event', 'AdBlock', 'True', {'nonInteraction': 1}); //event hit will not be used in bounce-rate calculation.
}else if(typeof _gaq !== 'undefined') {
_gaq.push(['_trackEvent', 'Adblock', 'True', undefined, undefined, true]); //event hit will not be used in bounce-rate calculation.
}
}else{
//Not detected
if(typeof ga !== 'undefined') {
ga('send', 'event', 'AdBlock', 'False', {'nonInteraction': 1}); //event hit will not be used in bounce-rate calculation.
}else if(typeof _gaq !== 'undefined') {
_gaq.push(['_trackEvent', 'Adblock', 'False', undefined, undefined, true]); //event hit will not be used in bounce-rate calculation.
}
}
};
window.onload = function() {
setTimeout(function() {
var AdBlockDetect = new AdblockDetector();
AdBlockDetect.run();
}, 3000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment