Connecting Magento to Google Tag Manager: Identifying Data Layer Variables
As an e-commerce developer looking to integrate your Magento store with Google Tag Manager (GTM), you’ll need to be familiar with the data layer variables that Magento provides out of the box. Understanding these variables is crucial for setting up custom events, triggers, and tracking pixels in GTM.
Understanding Magento’s Data Layer
Magento uses a JavaScript object called dataLayer to pass relevant information about products, categories, and user behavior from your storefront to Google Analytics (GA) or other third-party tools. This data layer is automatically populated with key-value pairs based on the current page context.
Identifying Core Magento Data Layer Variables
Although there isn’t a single, definitive list of all available Magento data layer variables, you can identify and extract them from your store’s JavaScript files. Here are some core variables that you’ll commonly find:
Product Information
product_sku: The stock keeping unit (SKU) of the current product.product_id: The unique identifier for the current product in Magento.product_name: The name or title of the current product.product_price_excl_tax: The price of the product before tax is applied.
Category Information
category_id: The unique identifier for the current category.category_name: The name or title of the current category.category_path: A hierarchical path representing the category’s location in the site tree.
User and Cart Information (when on the checkout page)
cart_items_count: The total number of items currently in the user’s cart.total_cart_price_excl_tax: The subtotal price of all items in the cart before tax is applied.guest_email: The email address provided by the guest user during checkout.
Extracting Data Layer Variables
To identify the data layer variables available on your specific Magento store, you can:
- Inspect the page’s JavaScript using browser developer tools (e.g., Google Chrome DevTools).
- Look for the
dataLayer.push()orwindow.dataLayer = window.dataLayer || [];lines in the rendered scripts. - Analyze the objects passed as arguments to these functions, which usually contain the relevant data layer variables and their values.
For example, on a Magento product page, you might find something like this:
dataLayer.push({
'ecommerce': {
'currencyCode': 'USD',
'detail': {
'products': [{
'name': 'Sample Product',
'id': '12345',
'price': '9.99',
'brand': 'Magento Commerce',
'category': 'Electronics > Smartphones'
}]
}
},
'event': 'pageview'
});
Setting Up Custom Events in GTM
Once you’ve identified the necessary data layer variables, you can use them to create custom events and triggers in your Google Tag Manager configuration:
- In GTM, navigate to the Variables section.
- Click on New > Variable > DataLayer Variable.
- Enter a name for the new variable (e.g.,
Product SKU). - Select the appropriate data layer variable from the dropdown menu that matches the key you identified in your store’s JavaScript.
By following this approach, you can effectively harness Magento’s data layer to enhance your tracking capabilities and gain deeper insights into user behavior within your e-commerce platform.
For more advanced customizations and integrations with Google Analytics 4 (GA4), consider checking out our comprehensive guide on Connecting Magento with GA4.