1

I want to add (GTM) google tag manager in my Magento 2 store, but without installing any extension.

Please help me out that how I can add CTM manually.

5 Answers 5

2

METHOD TO ADD GOOGLE TAG MANAGER TO MAGENTO 2 STORE

In Magento 2, you can easily add the Google Tag Manager without any need for a third-party extension.

To add Google Tag Manager to your Magento 2 store, all you need to do is add your unique Google Tag Manager code to your theme.

The Google Tag Manager code is divided into two parts – Script & NoScript.

You need to put these code snippets into two separate phtml template head and body files as shown below.

// YourTheme/Magento_Theme/templates/html/gtm_head.phtml

<script>..</script> <!-- GTM code -->

// YourTheme/Magento_Theme/templates/html/gtm_body.phtml

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

Now, we also need to load these files correspondingly through the default default.xml file.

<!-- YourTheme/Magento_Theme/layout/default.xml -->

<referenceContainer name="head.additional">

<block class="Magento\Framework\View\Element\Template" name="gtm.head" before="-" template="Magento_Theme::html/gtm_head.phtml" />

</referenceContainer>

<referenceContainer name="after.body.start">

<block class="Magento\Framework\View\Element\Template" name="gtm.body" before="-" template="Magento_Theme::html/gtm_body.phtml" />

</referenceContainer>

Your Google Tag Manager code is finally added and now you can set up your Google Analytics for monitoring and reporting of your Magento 2 store.

1

Open your Magento Admin Panel and move to Content > Design > Configuration > Edit the Store View > Other Settings> HTML Head.

Paste the code you got from GTM for the tag here.

1

You can easily add Google Tag Manager in Magento 2 without third-party extensions. All you need is to add the GTM code to your active theme. The GTM code is divided into two parts: script and noscript.

You need to put the GTM snippet into these two phtml templates for 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 -->

To load the files correspondingly you should use the default .xml file:

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

        <referenceContainer name="after.body.start">
            <block class="Magento\Framework\View\Element\Template" name="gtm.body" before="-" template="Magento_Theme::html/gtm_body.phtml" />
        </referenceContainer>
    </body>
</page>

NOTE: You must run the deploy command after adding the above files

1

The best way is to use some ready-made Magento 2 GTM extension, because if you just add Google Tag Manager (GTM) code to the HTML head, how would you like to integrate Google Analytics (GA), Google E-commerce Tracking, or Google Remarketing?

There are a lot of free and paid modules for that. So all you need to do is to install one:

  1. Magento 2 GTM Extension by WeltPixel - I used it a lot in the past, but it doesn't work with Google Analytics 4, so I recommend using the next one.

  2. Magento 2 Google Tag Manager Extension by Magefan - the best option for now as for me. It is also available on GitHub - https://magefan.com/magento-2-google-tag-manager.

  3. Magento 2 GTM Extension by Magepal - another option, if you don't like 2 previous ones.

So run these commands to get the GTM extension:

composer require magefan/module-google-tag-manager
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy

After that navigate to your Magento 2 admin panel > Stores > Configuration > Magefan Extensions > Google Tag Manager and Analytics > Configure and enable the GTM.

0

did you find a way to add the enhance e-commerce feature from ga4 using gtm to magento? in sales>google API > I add the tag but is not working the data layer

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.