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=""
    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

    let adBlockEnabled = false;
    const ad = document.createElement('div');
    ad.innerHTML = '&nbsp';
    ad.className = 'adsbox';
    window.setTimeout(function() {
        if (ad.offsetHeight === 0) {
            adBlockEnabled = true;
        if(adBlockEnabled == true) {
        console.warn('Blocking ads?', adBlockEnabled)
    }, 100);

Does anyone have any suggestions?

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