How to set up Google Tag Manager on a Magento site

Author:

Setting up Google Tag Manager (GTM) on a Magento site is a straightforward process that allows you to manage various tracking codes and marketing tags without modifying the website’s source code directly. Here’s a step-by-step guide to integrating GTM with your Magento store:

Step 1: Create a Google Tag Manager Account

  1. Sign Up for Google Tag Manager (GTM):
    • Go to Google Tag Manager.
    • If you don’t have a GTM account, create one by clicking on Create Account.
    • Enter your account name, country, and the container name (usually your website name or domain).
    • Select Web as the platform for the container.
    • Click Create, review and accept the terms of service.
  2. Get the GTM Container Code:
    • After creating your account and container, GTM will provide you with a code snippet.
    • The snippet contains two parts: one to be placed in the <head> section and another just after the opening <body> tag of your website.

Step 2: Install Google Tag Manager on Your Magento Site

There are a few different ways to install GTM on your Magento site. Here’s the most common method:

Method 1: Install GTM via the Magento Admin Panel

  1. Login to Magento Admin:
    • Log in to your Magento admin panel.
  2. Navigate to the Design Settings:
    • Go to StoresConfigurationGeneralDesign.
    • Under HTML Head, find the Scripts and Style Sheets section.
  3. Add the GTM Code Snippet to the <head> Section:
    • In the Scripts and Style Sheets section, locate the Other box. Paste the first GTM code snippet (the one for the <head> section) into this field.
  4. Add the GTM Code Snippet Right After the <body> Tag:
    • For the second part of the GTM code (the one to be placed after the opening <body> tag), you’ll need to modify your Magento theme files.
    • Go to ContentDesignThemes, and find your active theme.
    • Edit the default.xml file, which is located under app/design/frontend/[Vendor]/[Theme]/Magento_Theme/layout/ (create the folder if it doesn’t exist).
    • Add the following code right after the opening <body> tag:
    xml
    <head>
    <script>
    // Google Tag Manager snippet (second part)
    (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-XXXX');
    </script>
    </head>

    Note: Replace 'GTM-XXXX' with your actual GTM container ID.

  5. Save and Clear Cache:
    • After adding the code to your theme files, clear Magento’s cache by navigating to SystemCache Management, and then click Flush Cache Storage.

Method 2: Use a Magento Extension for GTM Integration

If you want an easier method, you can use a Magento extension to install Google Tag Manager.

  1. Install GTM Extension:
    • Search for Google Tag Manager extensions in the Magento Marketplace or install a popular extension such as “Google Tag Manager” by Mageplaza, Amasty, or a similar provider.
  2. Configure the Extension:
    • After installing the extension, go to the extension settings in your Magento admin panel (usually located under StoresConfigurationGoogle Tag Manager).
    • Enter your GTM Container ID in the appropriate field.
    • The extension should automatically insert the GTM code snippets into the header and body sections of your site.

Step 3: Verify Google Tag Manager Installation

Once you’ve installed the GTM container on your Magento site, you’ll want to verify that it is working properly.

  1. Use GTM Preview Mode:
    • In Google Tag Manager, click on Preview and enter your site URL to open the debug console. The console will show which tags are firing and which events are triggered when interacting with your site.
  2. Use Google Tag Assistant (Chrome Extension):
    • Install the Tag Assistant Chrome extension to verify that GTM is correctly implemented on your site. This tool will highlight any issues with the tags.
  3. Check Source Code:
    • View your website’s source code (right-click → View Page Source) and look for the GTM container code. You should see the GTM snippet in the <head> and <body> sections.

Step 4: Set Up Tags, Triggers, and Variables in GTM

Once GTM is installed, you can begin setting up tags for various tracking purposes. Some common e-commerce tracking tags include:

  1. Google Analytics:
    • Set up tags to track page views, transactions, and other user interactions.
  2. Google Ads Conversion Tracking:
    • Use GTM to track conversions from Google Ads, such as purchases or form submissions.
  3. Facebook Pixel:
    • Add a Facebook Pixel tag to track user behavior and conversions from Facebook ads.
  4. Custom E-commerce Events:
    • Use custom events to track interactions such as product views, add-to-cart, or checkout steps.

Example: Tracking Product Views in Google Analytics

  1. Create a New Tag for Google Analytics:
    • Go to TagsNew and select Google Analytics: GA4 Event.
    • Set the Event Name to view_item (for product views).
    • Under Event Parameters, map the necessary variables (like item_name, item_id, price, etc.) from the data layer.
  2. Set a Trigger for the Tag:
    • Create a trigger for the pageview or custom event that fires when a product page is loaded.
  3. Test and Publish:
    • Use the GTM preview mode to ensure your tags are firing correctly, and once confirmed, publish your changes.

Step 5: Maintain and Update Tags

As your Magento store evolves and you need to add new tracking, update your GTM tags and triggers. You can easily manage and update tags, triggers, and variables from within the GTM interface without needing to touch the Magento codebase.

Conclusion

Integrating Google Tag Manager with your Magento store is a powerful way to manage various marketing tags and tracking codes. Whether you implement GTM manually or use an extension, the key is ensuring that the GTM container is correctly placed on all pages of your site. Once set up, you can use GTM to track custom e-commerce events, measure user behavior, and optimize your online marketing efforts.