Created
December 26, 2017 23:51
-
-
Save OkoyaUsman/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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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