How to Set Up Google Tag Manager in Magento?

Published: (March 25, 2026 at 07:33 AM EDT)
2 min read
Source: Dev.to

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)

  1. In Magento Admin go to Content > Design > Configuration.
  2. Edit the desired theme and paste the GTM container script into Head > Scripts and Style Sheets.
  3. 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 to head.phtml.
  • Add the <iframe> snippet to body.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

  1. Create a GTM account and set up a container for your store.
  2. Copy the GTM Account ID, Container ID, and Public ID from the GTM dashboard.
  3. Open Google Analytics 4 and copy the Measurement ID of your property.
  4. In Magento admin, paste the GTM IDs and GA4 Measurement ID into the extension’s configuration fields.
  5. Import the data layer and create GTM tags as needed.
  6. Use GTM Preview mode to verify that tags fire correctly.
  7. 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.

0 views
Back to Blog

Related posts

Read more »