0

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="Magento\Framework\View\Element\Template"
           name="gtm.head"
           before="-"
           template="Magento_Theme::html/gtm_head.phtml" />
 </referenceContainer>
 ...

default.xml

...
<!-- Google Tag Manager -->
<referenceContainer name="after.body.start">
   <block class="Magento\Framework\View\Element\Template"
          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) -->
4
  • 1
    Could you add the content of the layout xml file and the file path of that file in your question to make us easy to investigate?
    – Tu Van
    Commented Nov 14, 2022 at 14:35
  • Thanks for your comment. I went through the code and found the mistake. I did write the HEAD-information to "default.xml" but it has to be integrated in "default:head_blocks.xml". I update the question – maybe someone else faces the same issue. Commented Nov 14, 2022 at 15:32
  • Great. Let's post an answer and mark it as accepted to help others who have the same issue :)
    – Tu Van
    Commented Nov 14, 2022 at 15:39
  • Yes I did, thanks again for finding the path. Commented Nov 14, 2022 at 15:46

1 Answer 1

0

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

See the whole solution above in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.