How to Set Up Google Tag Manager in Magento?
Source: Dev.to
Benefits of Google Tag Manager for Magento
- Integrate tools like GA4 and Google Ads for a full‑funnel view of customer behavior.
- Add and update tracking without redeploying the site, enabling faster updates and experimentation.
- Track product performance, identify cart‑abandonment points, analyze user journeys across pages, and aggregate data from different devices and browsers.
Manual Installation (Without Extensions)
- In Magento Admin go to Content > Design > Configuration.
- Edit the desired theme and paste the GTM container script into Head > Scripts and Style Sheets.
- Save the configuration.
Note: this only installs the GTM container; tags, triggers, and variables must still be configured in GTM.
Alternative: Insert GTM Directly in Theme Files
- Add the GTM
<script>snippet tohead.phtml. - Add the
<iframe>snippet tobody.phtml.
After the script is in place, Magento will load the GTM container automatically. GTM events (tags, triggers, variables) must be set up inside the GTM interface.
Using a GTM Extension (e.g., Magefan Google Tag Manager)
Extensions provide pre‑built data layers for eCommerce events and automatic connection to Google Analytics, eliminating the need to edit code or create tags manually.
General Steps with the Extension
- Create a GTM account and set up a container for your store.
- Copy the GTM Account ID, Container ID, and Public ID from the GTM dashboard.
- Open Google Analytics 4 and copy the Measurement ID of your property.
- In Magento admin, paste the GTM IDs and GA4 Measurement ID into the extension’s configuration fields.
- Import the data layer and create GTM tags as needed.
- Use GTM Preview mode to verify that tags fire correctly.
- Publish the GTM container to activate tracking.
After publishing, you can monitor real‑time activity in GA4 > Reports > Realtime.
Conclusion
Adding Google Tag Manager to Magento 2 enhances analytics and supports smarter decision‑making. Using a dedicated extension simplifies the setup, reduces errors, and speeds up implementation, especially when working with eCommerce events, data layers, and integrations such as Google Analytics 4.