Content mode, gtm, mulltidomain | WordPress.org

Greetings, I have some problems implementing google consent mode. But while trying to add this consent mode, I notices some problem.

Google tag manager seems to be loaded on page load, and when users accept cookies, it loads again. Since I added the GTM code in the Additional Cookies Head Section, it should only load when user accepts. Default status of that category is disabled.

As we have wpml multidomain setup this is the code that I use to switch gtm loading depending on domain:

var currentDomain = window.location.hostname;

var domainContainerMap = {

    'www.domain.com': 'GTM-xxxxxxx',

    'www.domain.de': 'GTM-xxxxxxx',

};

if (currentDomain in domainContainerMap) {

    var containerId = domainContainerMap[currentDomain];

    var noscriptIframe = document.createElement('noscript');

    var iframe = document.createElement('iframe');

    iframe.src="https://www.googletagmanager.com/ns.html?id=" + containerId;

    iframe.height="0";

    iframe.width="0";

    iframe.style.display = 'none';

    iframe.style.visibility = 'hidden';

    noscriptIframe.appendChild(iframe);

    document.body.appendChild(noscriptIframe);

    var script = document.createElement('script');

    script.innerHTML = "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l="+l:"';j.async=true;j.src="https://www.googletagmanager.com/gtm.js?id="+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','" + containerId + "');";

    document.head.appendChild(script);

}

I followed the implementation guide for google consent mode, but nothing seems to be working.
Any advice would be helpfull.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer