Magento's Tracking Landscape
Magento 2 (now Adobe Commerce) is an enterprise ecommerce platform with a different architecture from Shopify or WooCommerce. Its server-rendered pages, custom checkout flow, and extension ecosystem create specific considerations for ecommerce tracking.
GTM Installation on Magento 2
The recommended approach for most Magento stores is to install GTM via an extension:
- Official Google Tag Manager extension (available on Adobe Commerce Marketplace): installs the GTM container snippet in Magento's layout XML — the correct approach rather than modifying templates directly
- Community extensions (MagePal GTM, TagManager.co): open-source alternatives that add GTM and often include a built-in ecommerce data layer
Do not add GTM directly to Magento template (.phtml) files — this creates maintenance problems with upgrades. Always use the layout XML approach or an extension.
The Magento Data Layer Architecture
Magento 2 has a native JavaScript data layer implementation but it is designed for Adobe's own analytics (Adobe Analytics via Launch, not GA4). For GA4 ecommerce tracking, you typically need a custom data layer extension or custom development that pushes GA4-format events.
The core data layer events to implement:
- Category/product list pages: view_item_list on page load
- Product detail pages: view_item on page load
- Add to cart: add_to_cart via AJAX response handler
- Mini-cart/cart page: view_cart on cart view
- Checkout steps: begin_checkout, add_shipping_info, add_payment_info via Knockout.js step callbacks
- Order success page: purchase on Magento's success.phtml template
Checkout Implementation in Magento
Magento's checkout uses Knockout.js for its single-page checkout architecture. Tracking checkout steps requires hooking into Knockout's step completion callbacks:
// In a RequireJS module
define(['ko', 'Magento_Checkout/js/model/step-navigator'],
function(ko, stepNavigator) {
stepNavigator.registerStep('shipping', null, 'Shipping', true, function() {
// Fires when shipping step completes
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: 'add_shipping_info',
ecommerce: {
currency: 'GBP',
value: /* current cart total */,
shipping_tier: /* selected shipping method label */,
items: /* cart items array */
}
});
}, 20);
}
);
Order Success Page Implementation
Magento's order success page (checkout/success) provides order data via PHP. The standard approach is to render the GA4 purchase event data layer push directly in the success.phtml template or via a layout XML block:
<?php
$order = $block->getOrder();
if ($order && !$order->getData('ga4_tracked')) {
$items = [];
foreach ($order->getAllVisibleItems() as $item) {
$items[] = [
'item_id' => $item->getSku(),
'item_name' => $item->getName(),
'price' => (float)$item->getPrice(),
'quantity' => (int)$item->getQtyOrdered()
];
}
echo '<script>';
echo 'dataLayer.push({ecommerce:null});';
echo 'dataLayer.push(' . json_encode([
'event' => 'purchase',
'ecommerce' => [
'transaction_id' => $order->getIncrementId(),
'value' => (float)$order->getGrandTotal(),
'tax' => (float)$order->getTaxAmount(),
'shipping' => (float)$order->getShippingAmount(),
'currency' => $order->getOrderCurrencyCode(),
'items' => $items
]
]) . ');';
echo '</script>';
$order->setData('ga4_tracked', 1)->save();
}
?>
Adobe Commerce vs Community Edition
Adobe Commerce (formerly Magento Commerce) includes additional features (B2B, advanced pricing, etc.) that affect tracking: account-based pricing requires dynamic price values in product events; B2B quote workflows need custom events for quote requests; and requisition lists need custom add-to-cart event handling.
Summary
Magento/Adobe Commerce ecommerce tracking requires GTM installation via extension, custom data layer implementation for GA4-format events (either via a third-party extension or custom module), and careful handling of the Knockout.js checkout. Always use Magento's extension architecture for any tracking code rather than modifying core files. The purchase event deduplication flag (ga4_tracked) is especially important in Magento where order success pages can be revisited.
See our Enhanced Ecommerce Tracking service for Magento implementation.
Need GA4 ecommerce tracking for Magento? Contact Adslytics.
Need expert tracking setup?
Our Google Tag Manager experts have delivered 500+ tracking setups with a 98% success rate.
Get a Free Consultation →