
To drive sales and provide a personalised customer experience, every decision you make should be driven by data. That's what tools like Google Tag Manager come in handy.
To add Google Tag Manager in Magento means getting insights about customer behaviour, product performance, checkout and cart and using it in multiple marketing channels.
In this guide, you'll learn how to install the GTM script and how add Google Tag Manager to Magento using the Magefan .
- Installing the Google Tag Manager script in Magento is done via Content > Design > Configuration > Theme > Head > Scripts and Style Sheets.
- Adding the GTM script doesn't enable Google Analytics 4, you need to do it separately via Sales > Google API.
- By default, Magento supports onlythe GTM web container (GTM script).
- Adding the Google Tag Manager script to Magento doesn't create tags, triggers and variables automatically.
- To create events in GTM automatically, install Magefan Google Tag Manager Extension for Magento and export the pre-built data layer.
Before You Add Google Tag Manager
Before you install the GTM script in Magento, consider a few things:
- Create a GTM account and a GTM container
Note: if you consider adding server-side tracking in Magento, you need a separate server container.
- Make sure you have access to the theme settings and corresponding config sections (Content > Design > Configuration and Config > Sales > Google API)
- Check if you have permissions to preview tags inside your GTM account for testing.
- You should have a basic understanding of HTML and CSS when placing the GTM script inside your theme.
How to Install the Google Tag Manager Script?
To add Google Tag Manager to Magento without any extensions, just install the GTM script into your theme. Use the GTM script and no script parts and put them inside two separate phtml template head and body files.
The other option is to navigate to Magento Admin > Content > Design > Configuration and paste the GTM script inside the Head > Scripts and Style Sheets section of a specific theme.
Magento "Scripts and Style Sheets" section
Important: adding the GTM script doesn't enable Google Analytics. You have to add Google Analytics 4 in Magento separately after you set up GTM.
Once you install the GTM script, Magento will trigger the GTM script, but you have to set up GTM events — tags, triggers and variables — separately.
How to Add Google Tag Manager to Magento?
The easiest option to add Google Tag Manager to Magento is using an extension, like Magefan Google Tag Manager. You don't have to edit the script, work with code, set up GA4 separately or even create GTM tags.
Magefan GTM Extension comes with pre-built data layers for all important eCommerce events and connects to Google Analytics automatically.
Let's see how it works.
Step 1: Set up a Google Tag Manager account and container
Log in to Google Tag Manager account and create a new account if you don't already have one.
![]()
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.
![]()
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.
Step 2: Retrieve GTM Public, Account and Container IDs
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.
![]()
- Account ID
Go to the container page and find it in the browser address after /accounts/.
![]()
- Container ID
Open the container from the GTM homepage and find the container ID in the browser address after /containers/.
![]()
Step 3: Retrieve Measurement ID in Google Analytics
Since the Google Tag Manager extension needs to know where you send the gathered data, you need to get the 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.
![]()
Then move to Admin (gear icon) > Data Streams > Web stream details and find your Measurement ID.
![]()
Step 4: Paste GTM IDs into your Magento 2 admin
Note: If you've used the 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.
![]()
After that enable Google Analytics 4 and paste the Measurement ID there. Save the settings once you finish.
![]()
Step 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.
![]()
Once you save the file, go to the GTM homepage > Admin and click on Import 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).
![]()
Finally, you'll be given a preview of the new, modified and deleted GTM tags, triggers and variables to confirm.
![]()
Step 6: Check GTM connection
The fastest way to check if Google Tag Manager is working 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.
![]()
Then enter your Magento website URL in the appeared popup.
![]()
The URL will open in a new tab. If you've completed all of the steps correctly, you should see the following message:
![]()
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.
![]()
Step 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.
![]()
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.
![]()
Then, you'll see the Version Summary with the changes, date, description and the user who submitted it.
![]()
Finally, you can go to the GA4 > Reports > Realtime and monitor the activity on your website.
![]()
Magefan Google Tag Manager makes adding GTM in Magento faster and connects Google Analytics 4 out of the box. On top of that, it allows you to set up Conversion Tracking and Dynamic Remarketing.
But none of these guarantees your reports will be accurate. All because adding just the Google Tag Manager script has some serious drawbacks.
Drawbacks of Using a GTM Web Container
While using GTM in Magento brings flexibility and more control over your marketing tags, it's important to remember the limitations the GTM web container (script-based tracking) comes with.
Performance and SEO impact
Although the GTM script is lightweight, each tag you add to the container influences the loading times. This impacts the Core Web Vitals and other vital performance metrics, degrading your search rankings over time.
Data accuracy
Google Tag Manager script operates in the users' browsers, completely reliant on the client execution. This makes it prone to being blocked by privacy settings, ad blockrs and slow network connections. Because if the script doesn't run, no data is captured. This leads to incomplete, inaccurate or completely lost data.
Security and compliance issues
Any tracking tag you add to Magento through the GTM script runs in the user's browser. If not audited and executed properly, it makes your store exposed to security vulnerabilities or trigger behaviours that conflict with data privacy regulations. Always enable cookie consent in Magento to prevent that.
Google Tag Manager: Web or Server
Adding Google Tag Manager to Magento streamlines data tracking and makes it easier to manage data. Especially when you use the Magefan Google Tag Manager module to do that faster.
However, how you add GTM — via script or server — defines how safe, accurate and useful that data is going to be.
While the GTM web container has been a common standard for a long time, server-side tracking is a future-proof way of collecting data.
The best part is that you can combine both. So, consider using the GTM server container in Magento too.