Skip to content

Google tag manager crashes Magento 2.4.3-p2

I tried to integrate Google Tag Manager in M2.4.3-p2 with these instructions.
Unfortunately this causes that all standard body classes of M2 (f.e. “catalog-category-view”, “page-layout-2columns-left”) are not created in the frontend.

+++ SOLUTION: +++

I managed to find a solution: The problem was that I added the instructions for the HEAD in “default.xml” but it should be inserted in “default_head_blocks.xml” like this:

default_head_blocks.xml

...     
<!-- Google Tag Manager -->
 <referenceContainer name="head.additional">
    <block class="MagentoFrameworkViewElementTemplate"
           name="gtm.head"
           before="-"
           template="Magento_Theme::html/gtm_head.phtml" />
 </referenceContainer>
 ...

default.xml

...
<!-- Google Tag Manager -->
<referenceContainer name="after.body.start">
   <block class="MagentoFrameworkViewElementTemplate"
          name="gtm.body"
          before="-"
          template="Magento_Theme::html/gtm_body.phtml" />
</referenceContainer>
...

gtm_head.phtml

<!-- Google Tag Manager -->
<script>(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','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager —>

gtm_body.phtml

<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
        height="0" 
        width="0" 
        style="display:none;visibility:hidden">
</iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->