Google Tag Manager Data Layer implementation and Magento Shop

Google Tag Manager Data Layer implementation and Magento Shop

August 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Google Tag Manager Data Layer implementation and Magento Shop

Connecting Magento with Google Tag Manager: Implementing the Data Layer

As an e-commerce developer, tracking user behavior and sales data is crucial for optimizing your online store’s performance. One of the most effective ways to collect this data is by using Google Analytics in conjunction with your Magento platform.

Understanding the Data Layer Problem

You’ve already installed the Magento extension for Google Tag Manager (GTM), which correctly integrates the GTM snippet into your site. However, the issue lies in the fact that no data layer is being declared before the GTM script on the checkout success page (/checkout/onepage/success).

The data layer is a JavaScript object that resides at the top of your HTML document and provides structured data about events or transactions to your analytics tools. Without it, important e-commerce metrics like transaction values, product information, and conversion rates remain undefined in GTM.

Declaring the Data Layer Before the GTM Script

To solve this issue, you need to create a custom JavaScript file that declares the data layer for the checkout success page and include it above your GTM script. Here’s how:

  1. Create a new JavaScript file: In your Magento root directory, navigate to app/design/frontend/{your-theme}/template/page/ (create this folder if it doesn’t exist). Inside this folder, create a new file called data-layer.js.

  2. Populate your data layer: Open the data-layer.js file and add the following code:

var dataLayer = {
    'transactionId': '12345',
    'affiliation': 'Your Store Name',
    'revenue': '200.00',
    'tax': '20.00',
    'shipping': '15.00',
    'couponCode': 'DISCOUNT10',
    'currency': '$'
};
  1. Include the data layer file in your layout: Open app/design/frontend/{your-theme}/layout/page.xml and add the following XML block inside the <default> tag:
<block type="core/text" name="data-layer-script">
  <action method="setText">
    <text><![CDATA[<script type="text/javascript" src="//your-domain.com/path-to-data-layer.js"></script>]]></text>
  </action>
</block>
  1. Add the block to the checkout success page: Open app/design/frontend/{your-theme}/template/checkout/success.phtml and add the following line just above the <head> tag:
<?php echo $this->getLayout()->createBlock('core/text')->setText('<script type="text/javascript" src="//google.com/gtm.js?id=GTM-XXXXXX"></script>') ?>

Replace //google.com/gtm.js?id=GTM-XXXXXX with the actual GTM snippet for your account.

  1. Flush Magento cache: Navigate to “System > Cache Management” in your Magento admin panel and click “Flush Magento Cache”.

Conclusion

By following these steps, you’ve successfully created a custom data layer for your Magento checkout success page and ensured it’s declared before the Google Tag Manager script. This allows GTM to access structured e-commerce data like transaction values and product information.

Remember that every online store is unique, so always double-check your setup and adjust variables as needed according to your specific product catalog and pricing structure.

Additional Resources

For a more comprehensive guide on integrating Magento with Google Analytics using GTM, check out this detailed tutorial from LaravelCompany.

Happy tracking!

Checkout Catalog Performance Extensions

Our network