I'm using magento 2 and I'm trying to add the Google Tag Manager code (noscript and script parts) to my magento 2 installation , what's the best way to do it ?

I found this module to do it https://github.com/magepal/magento2-googletagmanager

But once I enable it and add the code , when I go to my website I get a 404 error

The page you requested was not found, and we have a fine guess why

And is the same even if i try to go to the admin panel , the only way to revert this is to disable the module from the database.

Does anyone has any idea why it acts like that ?


7 Answers 7


If you want to add Google Tag Manager without creating a module, you can add the gtm code from your active theme.

You can have two phtml templates to include the GTM snippet that needs to go to head and body.

// YourTheme/Magento_Theme/templates/html/gtm_head.phtml
<script>..</script> <!-- GTM code -->

// YourTheme/Magento_Theme/templates/html/gtm_body.phtml
<noscript>..</noscript> <!-- GTM code -->

You can use the default.xml file to load the above files.

<!-- YourTheme/Magento_Theme/layout/default.xml -->
<referenceBlock name="head.additional">
    <block class="Magento\Framework\View\Element\Template" name="gtm.head" before="-" template="Magento_Theme::html/gtm_head.phtml" />

<referenceContainer name="after.body.start">
    <block class="Magento\Framework\View\Element\Template" name="gtm.body" before="-" template="Magento_Theme::html/gtm_body.phtml" />
  • Adding Google Tag Manager code snippet to the header section of your Magento store may seem like the ideal and easy solution. But this will not be sufficient when integrating any third-parties tracking codes that require site-specific data from Magento (i.e AdWords conversion tracking, how do you get the items ordered, subtotal, tax, etc?). Take a look at how data our extension give you access to in GTM. See github.com/magepal/magento2-googletagmanager Aug 19, 2019 at 12:20

It depends upon what all tracking codes you are trying to achieve with GTM, even if you make the above extension working for your Magento 2 store still you might not be able to get the data layer you would need to implement your tracking codes.

If you are trying to achieve conversion tracking then that module will definitely help you but if you want to achieve more like implementing Facebook Dynamic Remarketing or Adwords Dynamic Remarketing or any other tracking which requires different events on different pages then you need to use the module which comes up with data layers which can support your implementation.

Here is the Google Tag Manager module I would recommend for Magento 2 -:


  • thanks for sharing this, do you have similar module for magento 1? Thanks
    – user55997
    Jan 29, 2018 at 16:00
  • 2
    here we go -: scommerce-mage.com/… Apr 2, 2018 at 22:49

There is also this module https://github.com/yireo/Yireo_GoogleTagManager2 that provides a data layer and it is free.


Try out this module: https://www.weltpixel.com/google-analytics-enhanced-ecommerce-tag-manager-magento-2.html/

It is free and it has a lot of built in functioanlities. You just have to configure it from the backend.


You can create a simple custom module with inside Vendor/Module/view/frontend/layout/default.xml

<referenceContainer name="after.body.start">
    <block class="Magento\Framework\View\Element\Template" name="google.tag.manager" before="-" template="Vendor_Module::html/tag-manager.phtml" />

where tag-manager.phtml is exactly your google tag manager snippet.

So you'll have that the tag manager snippet is added in all pages as first block after body tag open.


The google tag manager is a simple JavaScript snippet that you need to put on all customer facing pages (you dont need it on ajax pages or admin pages). I would suggest simply adding it to your <head> phtml template, its hardly worth installing an extension over that!

Magento 2: Adding Arbitrary HTML to the of Every Page? > Magento 2: Adding Arbitrary HTML to the <head> of Every Page?

  • 3
    Yes and no. It depends. Chances are you'll need a lot of additional info sent through the data layer (product view info, conversion info) to really take advantage of Google Tag Manager. Adding the JS snippet won't give you that.
    – Ryan Hoerr
    May 25, 2016 at 20:18

Try This Code






<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendoreName_GoogleTagManager"/>



<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="googletagmanager_head"  template="VendoreName_GoogleTagManager::gtm_head.phtml" />
        <referenceContainer name="after.body.start">
            <block class="Magento\Framework\View\Element\Template" name="googletagmanager_body" template="VendoreName_GoogleTagManager::gtm_body.phtml" />



<script>..</script> <!-- Add your script tag GTM code -->



<noscript>..</noscript> <!-- Add your noscript GTM code -->

Click here to download

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