How to Set Up Google Tag Manager For Magento 2?

11 minutes read

To set up Google Tag Manager for Magento 2, you need to follow these steps:

  1. Create a Google Tag Manager account: Go to the Google Tag Manager website and sign in with your Google account or create a new one if you don't have one.
  2. Set up a new container: Once you're logged in, click on "Create Account" and provide the necessary account name and container name for your Magento 2 website.
  3. Get the container code: After creating the container, Google Tag Manager provides you with a container code snippet. Copy this code as you'll need to add it to your Magento 2 website.
  4. Access your Magento 2 admin panel: Log in to your Magento 2 admin panel using your credentials.
  5. Install the Google Tag Manager extension: In your Magento 2 admin panel, go to System → Web Setup Wizard → Component Manager. Search for the Google Tag Manager extension and click on "Install."
  6. Configure the extension: Once the installation is complete, go to Stores → Configuration → Sales → Google API. Here, you can paste the container code you copied earlier in the "Google Tag Manager ID" field.
  7. Save the changes: Click on "Save Config" to save the updated Google Tag Manager configuration.
  8. Clear cache: Finally, go to System → Cache Management and clear the cache to ensure that the changes take effect.


Now you have successfully set up Google Tag Manager for your Magento 2 website. You can proceed with adding tags, triggers, and variables in the Google Tag Manager interface to track various events and actions on your website.

Best Google Analytics Books In 2024

1
Google Analytics Demystified (4th Edition)

Rating is 5 out of 5

Google Analytics Demystified (4th Edition)

2
Learning Google Analytics: Creating Business Impact and Driving Insights

Rating is 4.9 out of 5

Learning Google Analytics: Creating Business Impact and Driving Insights

3
Google Analytics: Understanding Visitor Behavior

Rating is 4.8 out of 5

Google Analytics: Understanding Visitor Behavior

4
Google Analytics Breakthrough: From Zero to Business Impact

Rating is 4.7 out of 5

Google Analytics Breakthrough: From Zero to Business Impact

5
Google Analytics Alternatives: A Guide to Navigating the World of Options Beyond Google

Rating is 4.6 out of 5

Google Analytics Alternatives: A Guide to Navigating the World of Options Beyond Google

6
Learning Google AdWords and Google Analytics

Rating is 4.5 out of 5

Learning Google AdWords and Google Analytics

7
Data Engineering with Google Cloud Platform: A practical guide to operationalizing scalable data analytics systems on GCP

Rating is 4.4 out of 5

Data Engineering with Google Cloud Platform: A practical guide to operationalizing scalable data analytics systems on GCP

8
Practical Google Analytics and Google Tag Manager for Developers

Rating is 4.3 out of 5

Practical Google Analytics and Google Tag Manager for Developers

9
Advanced Web Metrics with Google Analytics

Rating is 4.2 out of 5

Advanced Web Metrics with Google Analytics


How to track outbound link clicks using Google Tag Manager in Magento 2?

To track outbound link clicks using Google Tag Manager in Magento 2, follow these steps:

  1. Create a Google Analytics Tag in Google Tag Manager: Log in to your Google Tag Manager account. Select your container and navigate to "Tags" in the left sidebar. Click on "New" to create a new tag. Name the tag and click on "Tag Configuration". Choose "Google Analytics - Universal Analytics" as the tag type. Configure the tag with your Google Analytics tracking ID. Enable "Enable overriding settings in this tag" and select "Fields to Set". Add a new field with the following values: Field Name: "transport" Value: "beacon" Save the tag.
  2. Set up a trigger to fire the tag: Click on "Triggering" in the tag configuration. Click on "Choose a trigger to make this tag fire" and select "All Pages" trigger. Save the trigger and the tag.
  3. Enable Click Listener Variable in Google Tag Manager: Go to "Variables" in the left sidebar. Click on "Configure" under Built-In Variables. Enable the "Click URL" variable. Save the changes.
  4. Update the Magento 2 template files: Access your Magento 2 instance and go to your theme's directory. Edit the /Magento_Theme/templates/html/link.phtml file. Find the line that starts with , usually around line 20. Add the following line of code after the line mentioned above: getOutboundUrl($block->escapeUrl($block->getHref())); ?> Replace the existing line with the following one: href="" onclick="dataLayer.push({'event': 'outboundLink','outboundUrl': ''});"> Save the changes.
  5. Add a Custom JavaScript Variable to Google Tag Manager: Go to "Variables" in the left sidebar. Click on "New" to create a new variable. Name the variable as "gtm.linkClickOutbound". Choose the Variable Type as "Custom JavaScript". Paste the following JavaScript code into the "Variable Configuration" field: function() { return function() { return '{{Click URL}}'; }; } Save the variable.
  6. Create a Trigger to track outbound link clicks: Go to "Triggers" in the left sidebar. Click on "New" to create a new trigger. Name the trigger as "Outbound Link Click". Choose "Just Links" as the trigger type. Enable "Click URL" and set the condition to "matches CSS selector" and add a in the text box. Save the trigger.
  7. Update the Google Analytics Tag in Google Tag Manager: Go back to "Tags" in the left sidebar. Edit the existing Google Analytics tag. Add the trigger "Outbound Link Click" to the tag under "Triggering". Save the changes.


Once these steps are completed, Google Analytics will start tracking outbound link clicks on your Magento 2 website using Google Tag Manager.


What is the purpose of container versions in Google Tag Manager for Magento 2?

The purpose of container versions in Google Tag Manager for Magento 2 is to track and manage the changes made to the container. Containers are used to hold the various tags, triggers, and variables that are used to manage tracking and analytics within a website.


When a container is edited or updated in Google Tag Manager, a new version is created. These versions allow you to track, review, and revert changes made to the container. It ensures that you have a record of all the changes made, and you can easily go back to a previous version if needed.


Container versions also help in testing and debugging by allowing you to compare different versions and see the impact of changes on your website. It provides accountability and better control over the tracking and analytics implementations, ensuring that changes are properly documented and managed.


What is meant by data variables in Google Tag Manager for Magento 2?

In Google Tag Manager for Magento 2, data variables refer to the pieces of information that are used to collect and send data to various tags, such as Google Analytics, Facebook Pixel, or other marketing tools. These variables can be created and configured within Google Tag Manager to dynamically capture data from the Magento 2 platform.


Data variables can include information like the product ID, product name, category, price, transaction ID, customer ID, or any other relevant data points needed for tracking and analytics purposes. These variables can be utilized to customize the tracking and tagging implementation in Magento 2, allowing marketers and analysts to gather specific data about user interactions, conversions, and other metrics.


What is the recommended method for adding Google Tag Manager to a Magento 2 theme?

The recommended method for adding Google Tag Manager to a Magento 2 theme is as follows:

  1. Log in to your Magento 2 admin panel.
  2. Go to "Stores" and click on "Configuration."
  3. In the left sidebar, under "Sales," select "Google API."
  4. Expand the "Google Analytics" section and set "Enable" to "Yes."
  5. In the "Enable Content Experiments" drop-down, select "No."
  6. Under "Set up your Google Analytics," enter your "Account Number" and "Container ID" for Google Tag Manager.
  7. Save the configuration.


Now, you need to add the Google Tag Manager script to your theme files:

  1. Connect via FTP or SSH to your Magento 2 server.
  2. Navigate to your theme's directory, typically located at app/design/frontend/{Vendor}/{Theme}/Magento_Theme.
  3. Create a new folder called "layout" if it doesn't already exist.
  4. Inside the "layout" folder, create a new XML file, for example, "default.xml." This file determines where the script will be inserted into your theme.
  5. Open the XML file and add the following code:
1
2
3
4
5
6
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <script src="//www.googletagmanager.com/gtm.js?id=YOUR_CONTAINER_ID" src_type="url"/>
    </head>
</page>


Replace "YOUR_CONTAINER_ID" with your actual Google Tag Manager container ID.

  1. Save the XML file and clear Magento's cache.


By following these steps, you will have successfully added Google Tag Manager to your Magento 2 theme.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To track website tags with Google Tag Manager, follow these steps:Set up a Google Tag Manager account: Start by signing in to your Google account and access the Google Tag Manager website. Create a new account by providing the necessary details and setting up ...
To check your Google Tag Manager tags, you can follow these steps:Go to the Google Tag Manager website and log in with your Google account credentials. Once logged in, you&#39;ll be directed to the Google Tag Manager dashboard. Select the container you want to...
Google Tag Manager (GTM) is a powerful tool that allows you to manage and deploy various tracking tags on your website without directly editing the code. Here&#39;s a step-by-step guide on how to use Google Tag Manager:Sign in to your Google Tag Manager accoun...