How to add Google Tag Manager to Magento 2?
- Triggers - Trigger identifies when and where the tags are to be executed.
- Variables - Variables receive and keep information used by tags and triggers.
How to add Google Tag Manager in Magento 2?
You can easily add GTM 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:
<script>..</script> <!-- GTM code -->
<noscript>..</noscript> <!-- GTM code -->
To load the files correspondingly you should use the default .xml file:
<!-- YourTheme/Magento_Theme/layout/default.xml -->
<block class="Magento\Framework\View\Element\Template" name="gtm.head" before="-" template="Magento_Theme::html/gtm_head.phtml" />
<block class="Magento\Framework\View\Element\Template" name="gtm.body" before="-" template="Magento_Theme::html/gtm_body.phtml" />
After GTM is added you can set up Google Analytics and other tools for tracking and reporting about website traffic.