Skip to content

Instantly share code, notes, and snippets.

@alyatwa
Created December 29, 2019 20:32
Show Gist options
  • Save alyatwa/3bcd687c9b4ed659a17076a27be7a181 to your computer and use it in GitHub Desktop.
Save alyatwa/3bcd687c9b4ed659a17076a27be7a181 to your computer and use it in GitHub Desktop.
detect uBlock/Adblock and send data to google analytics as event with proxy
document.addEventListener('DOMContentLoaded', init, false);
function init(){
adsBlocked(function(blocked){
if(blocked){
console.log('IsAdsBlocked: true');
jQuery.post( "https://elegant-benz-28d755.netlify.com/proxy/https://www.google-analytics.com/collect",
`v=1&t=event&tid=UA-XXXX-1&cid=555&ec=Ad%20Setting&ea=Adblock&el=Enabled&dt=${jQuery(document).attr('title')}&dp=${jQuery(location).attr('href')}`
).done(function( data ) {
console.log(data);
}, "application/x-www-form-urlencoded");
} else {
console.log('IsAdsBlocked: false');
}
})
}
function adsBlocked(callback){
var testURL = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
var myInit = {
method: 'HEAD',
mode: 'no-cors'
};
var myRequest = new Request(testURL, myInit);
fetch(myRequest).then(function(response) {
return response;
}).then(function(response) {
console.log(response);
callback(false)
}).catch(function(e){
console.log(e)
callback(true)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment