How to add Google Tag Manager to Magento 2
Google Tag Manager is a free tag management platform that enables you to add, update, and control marketing and analytics tags - such as tracking scripts and pixels on your website or mobile application without repeatedly editing the site’s source code Rather than manually embedding tools like Google Analytics, Facebook Pixel, Google Ads, or Hotjar directly into your website files, you install a single GTM container snippet once. After that, all tracking and marketing tags can be configured and managed through GTM’s web-based interface.
There are multiple ways to add Google Tag Manager to your Magento 2 store both free and paid and let's review each one of them.
METHOD 1 - Add Google Tag Manager to Magento 2 for free, no third party extension required
If you've installed Google Analytics 4 or the already deprecated Universal Analytics you might know that this comes down to adding a small Javascript snippet in every page in your site. With Google Tag Manager the process is absolutely the same and involves the following steps.
- 1. Go to Google Tag Manager an open a new account or login to your existing one
- 2. Once done, from the Tag Manager interface go to Admin and select Install Google Tag Manager
- 3. You shall see 2 Javascript snippets, one for HEAD section in your site and one for the BODY. The one that matters most is the HEAD one, this is where Javascript is acually executed. Copy this script
- 4. Go to your Magento 2 admin panel, then select Content / Design / Configuration.
- 5. Click edit on your Store view
- 6. Scroll down to HTML Head section and click to expand it
- 7. Paste the copied HEAD snippet in the Scripts & Styles textarea input
- 8. Save configuration
- 9. Go back to System / Cache management and flush cache.
That's all.
METHOD 2 - Add Google Tag Manager to Magento 2 using a ready GA4/GTM extension by Anowave
The real strength of Google Tag Manager is called dataLayer[] object. This is a special Javascript object that Google Tag Manager interacts with and performs different actions based on the data available in this object. For example, whenever customer adds item to cart, or goes to checkout, dataLayer[] object receives an event with additional paramaters such as the items added to cart or the items available at checkout. This dataLayer object however is not initialized automatically. Having GTM installed in HEAD is just one part of the equation, it can track basic page views and visits but it will not know about important ecommerce marketing events.
Here our ready GTM & GA4 extension for Magento 2 comes to the rescue
It can not only add Google Tag Manager to Magento 2 but it will also initialize dataLayer object automatically, it will create all required tags, triggers and variables in Google Tag Manager automatically as well.
To add Google Tag Manager to Magento 2 with our extension, the steps include:
- 1. Go to Google Tag Manager an open a new account or login to your existing one
- 2. Once done, from the Tag Manager interface go to Admin and select Install Google Tag Manager
- 3. You shall see 2 Javascript snippets, one for HEAD section in your site and one for the BODY. The one that matters most is the HEAD one, this is where Javascript is acually executed. Copy this script
- 4. Go to Stores / Configuration / Anowave / Google Tag Manager GA4
- 5. Expand the Google Tag Manager section and insert the HEAD snippet