GA4 and Google Tag Manager on Magento 2: E-commerce Events and Consent
When I take over a Magento 2 store that’s already live, one of the first things I check is tracking. Very often I find GA4 installed “halfway”: visits come in but purchases don’t, or they come in twice, or nobody can say whether they respect cookie consent. The result is that marketing decisions get made on the wrong numbers.
In this guide I explain what the core of Magento Open Source 2.4.x (and therefore Mage-OS too, which starts from the same code base) really offers for Google Analytics 4 and Google Tag Manager, when you need a module, how to build a correct e-commerce dataLayer, how to handle Consent Mode v2 and how to check that everything works.
1. What’s in the Magento Open Source core
Since 2.4.5 the core’s Google integration uses the gtag.js APIs, through the Magento_GoogleGtag module. The configuration is under Stores → Configuration → Sales → Google API → Google GTag, in the Google Analytics4 group: set Enable to Yes and enter the Measurement ID (the one starting with G-). The same group also holds the Google Ads part (Conversion ID and Conversion Label).
Looking at the module’s code, though, you immediately see the limit: the core sends the page view and the purchase event on the order success page. It doesn’t send view_item, add_to_cart, begin_checkout and the other funnel events. On top of that, loading is tied to Magento’s native Cookie Restriction Mode, not to Consent Mode v2 signals.
And Google Tag Manager? It isn’t in the Open Source core. The GTM module with Enhanced Ecommerce (Magento_GoogleTagManager) is documented by Adobe as a feature exclusive to Adobe Commerce: on Open Source and Mage-OS you need a third-party module or a custom implementation.
| Feature | Open Source / Mage-OS (core) | Adobe Commerce |
|---|---|---|
| GA4 via gtag.js | Yes (page view + purchase) | Yes |
| Google Ads conversion | Yes | Yes |
| Google Tag Manager | No, you need a module | Yes |
| Funnel events (view_item, add_to_cart…) | No | Yes, via GTM |
| Consent Mode v2 | No, it has to be handled separately | To be checked case by case |
2. Core gtag or Google Tag Manager?
If all you need is to know how many visits and orders the site gets, the core module can do the job. As soon as you want to analyze the funnel, measure campaigns across several platforms (Google Ads, Meta, Microsoft Ads) or handle consent properly, I recommend GTM: Magento just publishes the data to the dataLayer and all the tag logic lives in the container, which you can change without a deploy.
One important rule: don’t keep both active. If you use GTM with a GA4 tag, turn off GA4 in Google GTag, otherwise you end up with duplicate page views and purchases. It’s the mistake I see most often.
3. The module options
There are several GTM/GA4 modules for Magento 2 on the market, free and paid. When I evaluate one I always look at these points:
- Events covered: at least the whole funnel, from
view_item_listtopurchase. - Theme compatibility: Luma, Hyvä, Breeze or a custom theme, and with the checkout you use.
- Consent Mode v2: whether it handles the default consent or leaves it to the cookie banner.
- Maintenance: recent updates and support for the latest 2.4.x.
Among the free ones, one of the most widely used is Yireo_GoogleTagManager2, open source, with GA4 support and compatible with Luma and Hyvä (with additional modules for Hyvä Checkout and Loki Checkout).
At Threecommerce we have our own GA4 events module for GTM coming soon, designed for Magento 2 and Mage-OS: you’ll find it among the modules announced in the Threecommerce e-commerce area and, when it’s ready, in the module catalog.
4. GA4 e-commerce events and the dataLayer
GA4 has a set of recommended e-commerce events with precise names and parameters. If you follow them, the monetization reports fill in on their own. These are the ones a Magento store should send:
| Event | Where it fires in Magento |
|---|---|
view_item_list | Category page, search results, product widgets |
select_item | Click on a product in a list |
view_item | Product page |
add_to_cart / remove_from_cart | Adding to or removing from the cart (also from the minicart, via AJAX) |
view_cart | Cart page |
begin_checkout | Entering the checkout |
add_shipping_info / add_payment_info | Choosing the shipping and payment method |
purchase | Order success page |
Each event is published to the dataLayer with an ecommerce object. It’s good practice to clear the previous object first, so that one event’s data doesn’t get mixed with the next one’s:
window.dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: 'add_to_cart',
ecommerce: {
currency: 'EUR',
value: 49.90,
items: [{
item_id: 'SKU-123',
item_name: 'Zaino da trekking 30L',
item_brand: 'Marca',
item_category: 'Zaini',
price: 49.90,
quantity: 1
}]
}
});A few Magento-specific points to watch:
- Configurable products: decide whether
item_idis the parent’s SKU or the variant’s, and use the same logic in all events and in the Merchant Center feed. - Prices including or excluding VAT: pick a convention and stick to it. In
purchasealso passtaxandshipping. - Full page cache: cart and customer data must not be written into the cached HTML, but read client-side (customer data / private sections), otherwise everyone sees the first visitor’s cart.
transaction_id: use the order number (increment ID). GA4 uses it to deduplicate purchases if the success page is reloaded.
5. Consent Mode v2 and the cookie banner
If you have traffic from the European Economic Area, Consent Mode v2 isn’t optional if you use Google Ads and remarketing. I covered it in detail in the article on Consent Mode V2; here is a summary of what matters on Magento.
The default consent (everything denied) must be set before the GTM snippet loads, so at the top of the <head>:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
wait_for_update: 500
});Then the cookie banner, when the user makes a choice, sends a gtag('consent', 'update', {...}). Many consent management platforms (CMPs) already do this, through a GTM template or their own script: in that case don’t duplicate the default in the theme. Two practical tips:
- If you use an external CMP, turn off Magento’s Cookie Restriction Mode (Stores → Configuration → General → Web → Default Cookie Settings), otherwise you get two banners or two logics that contradict each other.
- If Magento’s Content Security Policy is in restrictive mode, add the GTM, GA4 and CMP domains to the whitelist.
6. Hyvä: what changes
Hyvä uses neither RequireJS nor jQuery, so a module written only for Luma often doesn’t work as is: the scripts that intercept add to cart or checkout have to be rewritten. Before installing a module, check that it declares Hyvä compatibility (or that a compatibility module exists in the Hyvä module tracker), and check the checkout too: Hyvä Checkout, the Luma checkout or a third-party one page checkout generate events in different ways.
7. Testing with Tag Assistant and DebugView
Never take tracking for granted without having tried it on a real (or test) order. This is my verification routine:
- In GTM click Preview: Tag Assistant opens, connected to the site.
- Browse a category, a product, the cart and the checkout, and check that each event appears with its complete
ecommerceobject. - Check the consent state before and after accepting the banner.
- In GA4 open DebugView and check that the events arrive with the right parameters.
- Complete a test order and compare value, tax, shipping and
transaction_idwith the order in Magento.
Checklist
- A single place where GA4 is installed: either the core Google GTag or GTM.
- Full funnel: view_item_list, view_item, add_to_cart, begin_checkout, purchase.
- Fixed conventions for SKUs, variants and prices with or without VAT.
- Default consent before GTM, updated by the cookie banner.
- Module compatible with your theme (Hyvä included) and your checkout.
- Tested with Tag Assistant, DebugView and a real order.
Well-done tracking is the foundation of any reasoning about campaigns and conversions. If you want me to check how your Magento store is set up, or you need a hand setting up GTM and Consent Mode, get in touch.