GrahamMorbyDev
1 month ago

Stop GTM running if Adblocker is present

Posted 1 month ago by GrahamMorbyDev

Im loading GTM into the head of my site with an iframe

<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TAG"
    height="0" width="0" style="display:none;visibility:hidden" id="gtmanager"></iframe></noscript>

Now if an ad blocker is present I want to remove it or stop it loading, I have a function I'm loading at the bottom of the page, but it seems as tho because it runs before the DOM is loaded I can't access the iframe itself

<script>
    let adBlockEnabled = false;
    const ad = document.createElement('div');
    ad.innerHTML = '&nbsp';
    ad.className = 'adsbox';
    document.body.appendChild(ad);
    window.setTimeout(function() {
        if (ad.offsetHeight === 0) {
            adBlockEnabled = true;
        }
        if(adBlockEnabled == true) {
            console.log('ragnar')
        }
        ad.remove();
        console.warn('Blocking ads?', adBlockEnabled)
    }, 100);
</script>

Does anyone have any suggestions?

Please sign in or create an account to participate in this conversation.