Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OMEGAYALFA/fb5d9c8b29e2998450d65908d8bbe809 to your computer and use it in GitHub Desktop.
Save OMEGAYALFA/fb5d9c8b29e2998450d65908d8bbe809 to your computer and use it in GitHub Desktop.
Detect Adblock using user and log in Analytics
// Found here: http://www.labnol.org/internet/adblock-with-google-analytics/28819/
window.onload = function() {
// Delay to allow the async Google Ads to load
setTimeout(function() {
// Get the first AdSense ad unit on the page
var ad = document.querySelector("ins.adsbygoogle");
// If the ads are not loaded, track the event
if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
if (typeof ga !== 'undefined') {
// Log an event in Universal Analytics
// but without affecting overall bounce rate
ga('send', 'event', 'Adblock', 'Yes', {'nonInteraction': 1});
} else if (typeof _gaq !== 'undefined') {
// Log a non-interactive event in old Google Analytics
_gaq.push(['_trackEvent', 'Adblock', 'Yes', undefined, undefined, true]);
}
}
}, 2000); // Run ad block detection 2 seconds after page load
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment