创建一个ads.js
使用以下代码的文件。
isAdBlockActive = false;
script.js
在您的页面中包含要检查广告拦截器的位置。我们isAdBlockActive
在下面的代码中检查变量,并检查Google Ads创建的元素ins.adsbygoogle
。如果未定义变量或未创建元素,则用户正在屏蔽广告,我们会在GA上显示模式和跟踪。现在,如果您不使用Google Ads,则只需检查变量是否在中定义script.js
。
var AdBlocker = (function () {
function showModal() {
$('#modal_ad_blocker').modal(); // show a message to the user when ads are blocked
}
setInterval(function () {
// Get the first AdSense ad unit on the page
var ad = document.querySelector("ins.adsbygoogle");
// If the ads.js or the Google ads are not loaded, show modal and track the event
if (typeof isAdBlockActive === 'undefined'
|| (ad && ad.innerHTML.replace(/\s/g, "").length === 0)) {
showModal();
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]);
}
}
}, 5000); // check every 5 seconds
})();
作者:拉姆·帕特拉
网址:https://blog.rampatra.com/how-to-know-whether-a-user-is-using-an-adblocker