3

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 ?

12

6 Answers 6

19

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" />
</referenceBlock>

<referenceContainer name="after.body.start">
    <block class="Magento\Framework\View\Element\Template" name="gtm.body" before="-" template="Magento_Theme::html/gtm_body.phtml" />
</referenceContainer>
1
  • 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 Commented Aug 19, 2019 at 12:20
2

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

2

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.

2

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" />
</referenceContainer>
...

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.

0

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?

1
  • 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
    Commented May 25, 2016 at 20:18
0

Try This Code

app/code/VendoreName/GoogleTagManager

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendoreName_GoogleTagManager',
    __DIR__
);

app/code/VendoreName/GoogleTagManager/etc

module.xml

<?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"/>
</config>

app/code/VendoreName/GoogleTagManager/view/frontend/layout

default.xml

<?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">
    <body>
        <referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="googletagmanager_head"  template="VendoreName_GoogleTagManager::gtm_head.phtml" />
        </referenceBlock>
        <referenceContainer name="after.body.start">
            <block class="Magento\Framework\View\Element\Template" name="googletagmanager_body" template="VendoreName_GoogleTagManager::gtm_body.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/VendoreName/GoogleTagManager/view/frontend/templates

gtm_head.phtml

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

app/code/VendoreName/GoogleTagManager/view/frontend/templates

gtm_body.phtml

<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.