How to Add Google Tag Manager to Magento 2? [Simple Steps]

Managing an eCommerce business is always about making decisions. Unfortunately, sometimes the outcome doesn't turn out as expected simply because the decisions are based on intuition. It's not how eCommerce works.

To drive sales and provide a personalized customer experience every decision you make should be driven by data about pages customers visits, products they buy, and many other metrics.

You don't expect you can collect that data without any help, do you?

So, in this guide, we'll help you set up Google Tag Manager for better data tracking with the help of a robust Magento 2 Google Tag Manager. You'll also find a video walkthrough later in this article.

But let's cover what Google Tag Manager is first.

What is Google Tag Manager?

Google Tag Manager (GTM) is a free tag management system developed by Google to help you to manage your store marketing tags from one interface without any code changes.

The tool allows you to deploy website tracking tags in one place which eliminates the need for constant code updates and possible errors.

Moreover, you don't need to apply the tag manually or have any technical knowledge of JavaScript. GTM allows you to add a container tag to your website and make all changes and debugging directly from the app.

And it's not only Google Analytics tracking tags you can set up. These could be tags from any third-party application like Facebook Pixel, Ads, Microsoft Bing, and others.

GTM works with 3 elements:

  • Tags— pieces of code implemented in your page structure (mainly JS code)
  • Triggers— define where and when to execute the tracking tags
  • Variable— stores information used by triggers and tags

According to these elements, there are defined 3 stages Google Tag Manager works in.

First, there occurs an event on your website (e.g. customers add products to cart). Then, the configured trigger acknowledges the event. This signals for the tracking tags to take certain actions.

Benefits of Google Tag Manager

In case some of you still do not see when it comes in handy, we want to list the benefits Google Tag Manager offers you:

  • GMT helps you to personalize your marketing campaign
  • It eliminates the need to call in a developer every time you need to make changes
  • No error appears during the tags implementation since they are added by the app, not manually
  • GMT with all the data scraped is a great asset to Google Analytics

Once you set up Google Tag Manager, you'll be able to continue this list since the amount of data it allows you to gather is invaluable.

How to Add Google Tag Manager to Magento 2?

To set up Google Tag Manager in Magento with the Magefan Google Tag Manager you won't need to edit any HTML templates or get tracking codes. We'll simply get the tracking IDs from GTM and GA4 and paste them into the admin

We'll work with the browser-based (client-based) tracking in this guide. However, you can also enable server-side tracking in Magento using Measurement Protocol or GTM server container.

Eager to start?

1. Set up a Google Tag Manager account and GTM Container

Log in to Google Tag Manager account and create a new account if you don't already have one.

Create Google Tag Manager Account

Then, set the Account Name and select Country. Specify the Container name which is a descriptive title used to associate the container with your website and select Web for a Target Platform.

Google Tag Manager Configuration

Note: since each container is associated with one website you need only one container in your account. In case you manage a multi-website Magento, you need separate containers.

2. Get your GTM Public ID, Account ID and Container ID

To get any of the IDs required for setting up the Google Tag Manager for Magento, you first need to navigate to the GTM homepage.

  • Public ID

Click on the Container name while on the GTM homepage. Then switch to the Admin tab and find it near the container name. 

GTM Public ID

  • Account ID

Go to the container page and find it in the browser address after /accounts/.

GTM Account ID

  • Container ID

Open the container from the GTM homepage and find the container ID in the browser address after /containers/.

GTM Container ID

3. Get Google Analytics 4 Measurement ID

Since Magento 2 Google Tag Manager extension needs to know where you send the gathered data, you have to get GA4 measurement ID and specify it in the extension settings.

First, go to the GA homepage and choose the GA4 property you'd like to use to see reports on your store performance. 

Google Analytics 4 Property

Then move to Admin (gear icon) > Data Streams > Web stream details and find your Measurement ID.

Google Analytics Measurement ID

4. Paste GTM IDs into your Magento 2 store

Note: If you've used GTM tracking script before, go to Content > Design > Configuration, find a website you used the script for and remove it from the HTML Head > Scripts and Style Sheets section.

Navigate to Stores > Configuration > Magefan Extensions > Google Tag Manager > Web Container and paste all the GTM IDs into this section. 

Set up Google Tag Manager in Magento 2

Note: if you use Stape.io or any other custom GTM loaders, choose Head Script and Body Noscript in the Installation Method field.

After that enable Google Analytics 4 and paste the Measurement ID there. Save the settings once you finish.

Google Analytics in Magento 2

Note: based on your plan for the Google Tag Manager Extension, you'll be able to gather different data. Magento 2 Google Tag Manager Plus allows you to track more events.

5. Create GTM tags

To track events in your Magento store you need to create tags, triggers and variables in your GTM account. But since Magefan Google Tag Manager for Magento 2 makes things easier, all you need is to import the preset tags from Magento. 

For that, go to Stores > Configuration > Magefan Extensions > Google Tag Manager > Export Web Container and click on the Generate JSON Container & Download File button.

export web container magento

Once you save the file, go to the GTM homepage > Admin and click on Import Container.

Import Google Tag Manager Container

Then select a file to import, choose workspace and set whether to overwrite or merge the selected workspace with the selected container and select a workspace (it is usually default).

Import JSON container into GTM

Finally, you'll be given a preview of the new, modified and deleted GTM tags, triggers and variables to confirm.

Updated GTM Tags and Triggers

6. Check GTM connection

The fastest way to check GTM tags on your store pages is to go to any of them, press Ctrl+U and view the source code. 

However, you can also check if the implementation worked from your Tag Manager Account by clicking Preview when in Workspace.

Preview Google Tag Manager

Then enter your Magento website URL in the appeared popup.

Connect tag assistant to your website

The URL will be opened in a new tab. If you've completed all of the steps correctly you should see the following message:

Connect Tag Assistant with your store

Click Continue to see the page with all monitored tags in Tag Assistant. 

Note: if you want to check some particular tags, go to your store, trigger the action and monitor the tags fired in this panel.

Tag assistant panel

7. Publish GTM tags

Important: every time you import and preview the changes in GTM you need to publish them. 

Go to the workspace panel tab and click the Submit button. 

Submit changes to Google Tag Manager

You'll be taken to the submission configuration panel where you can set the Version Name and Version Description. These options are entirely for your convenience. So, you can leave them empty and click Publish

Submit changes to GTM

Then, you'll see the Version Summary with the changes, date, description and the user who submitted it.

New tags version in GTM

Finally, you can go to the GA4 > Reports > Realtime and monitor the activity on your website.

Google analytics report

You're all set. You've added Google Tag Manager to Magento 2 fast and easily.

Now GTM will make your marketing efforts pay off with more personalized campaigns and improved conversion rates. You will now be able to track almost every action customers take in your store, see what products they view, add to their cart and purchase the most often. 

But that's not it. 

Magento 2 Google Tag Manager from Magefan allows you to set up Conversion Tracking and Dynamic Remarketing. So you can use the in-depth reports you get to drive more conversions through personalized ads.