Sub topic 11
Implementing E-commerce Events with the GTM dataLayer
Google Tag Manager (GTM) lets the shop's code describe what happened and lets GTM decide which tags send it to GA4. This chapter assumes you know the items array, the currency and value parameters, and the event names from earlier chapters. It also assumes a GTM web container is already installed on the site, and that you can open GTM's Preview mode.
How the dataLayer fits in
The dataLayer is a JavaScript array named window.dataLayer. Your page code pushes plain objects onto it. GTM reads each push, merges it into its internal data model, and checks whether any trigger matches.
An object with an event key fires triggers. An object without one only updates the data model. For e-commerce, you push one object that carries both the event name and an ecommerce object.
Pushing an event
Each push uses the same shape as the gtag.js parameters, nested under ecommerce. This push sends a view_item event:
window.dataLayer = window.dataLayer || [];
// Clear the previous ecommerce object so values from an earlier push do not carry over.
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: 'view_item',
ecommerce: {
currency: 'USD',
value: 24.99,
items: [
{
item_id: 'SKU_1001',
item_name: 'Trail Running Shoe',
price: 24.99,
quantity: 1
}
]
}
});Why the reset push matters
GTM merges new pushes into its data model recursively. It does not replace the old ecommerce object. Suppose a product page pushes view_item with items, and a later push on the same page sends add_to_cart with a coupon field. Without a reset, the add_to_cart event could still carry fields from the earlier push. Pushing { ecommerce: null } first clears the object.
Put the two pushes in a small helper so no developer forgets the reset:
window.dataLayer = window.dataLayer || [];
function pushEcommerce(eventName, ecommerce) {
dataLayer.push({ ecommerce: null });
dataLayer.push({ event: eventName, ecommerce: ecommerce });
}
pushEcommerce('add_to_cart', {
currency: 'USD',
value: 49.98,
items: [
{ item_id: 'SKU_1001', item_name: 'Trail Running Shoe', price: 24.99, quantity: 2 }
]
});Declare window.dataLayer = window.dataLayer || []; before the GTM snippet. Pushes made before the container loads are queued and processed in order once it does.
Configuring GTM
You need two pieces in the container for each event, or one pair that covers several events.
- A trigger. Create a trigger of type Custom Event. Set the event name to match the
eventvalue exactly, for exampleview_item. The match is case-sensitive. Use the regex option, such asview_item|add_to_cart|begin_checkout|purchase, if you want one trigger for several events. - A GA4 event tag. Create a Google Analytics: GA4 Event tag and select your Google tag or measurement ID. Set the event name to the GA4 name, such as
view_item. Under More Settings, then Ecommerce, tick Send Ecommerce data and choose Data Layer as the source.
With that option enabled, the tag reads currency, value, transaction_id, items and the other standard fields directly from the ecommerce object. You do not add them as event parameters by hand. The tag still needs the event name. If you use {{Event}}, the built-in variable that holds the event value from the push, one tag can serve every e-commerce event.
If you want a field in the tag but not in the data layer, add it as an event parameter on the tag. Parameters set on the tag override the data layer values with the same name.
Sending a purchase
The purchase event follows the same pattern. The risk with purchases is duplication. A confirmation page that the user reloads, bookmarks, or reopens from an email will run the push again. Each push is a new event, and revenue is counted again.
window.dataLayer = window.dataLayer || [];
function pushEcommerce(eventName, ecommerce) {
dataLayer.push({ ecommerce: null });
dataLayer.push({ event: eventName, ecommerce: ecommerce });
}
function pushPurchaseOnce(order) {
const key = 'ga_purchase_' + order.transaction_id;
if (localStorage.getItem(key)) {
return; // This order was already reported from this browser.
}
pushEcommerce('purchase', order);
localStorage.setItem(key, '1');
}
pushPurchaseOnce({
transaction_id: 'T_12345',
currency: 'USD',
value: 49.98,
tax: 4.00,
shipping: 5.99,
coupon: 'SUMMER_SALE',
items: [
{ item_id: 'SKU_1001', item_name: 'Trail Running Shoe', price: 24.99, quantity: 2, index: 0 }
]
});GA4 also uses transaction_id to discard repeat purchase events, but treat that as a backup. Do not rely on it alone.
Common errors
| Symptom | Likely cause | Column | Column |
|---|---|---|---|
| Tag never fires in Preview | The `event` value differs from the trigger by case or spelling, or the push has no `event` key. | ||
| Items from a previous page view appear on a new event | The `{ ecommerce: null }` reset is missing. | ||
| Event arrives with no items or value | **Send Ecommerce data** is not ticked, or the push puts `items` at the top level instead of under `ecommerce`. | ||
| Every event is sent twice | A hard-coded `gtag('event', ...)` call and a GTM tag both send the same event. Use one method. | ||
| Purchase counted again on reload | The confirmation page pushes unguarded on every load. | ||
| Push is ignored or errors | A script overwrote `window.dataLayer` with a new array after GTM loaded. Always use `window.dataLayer = window.dataLayer | []`. | |
| Price appears as a string | The page pushes `'24.99'` rather than `24.99`. Send numbers, not strings. |
Single-page apps
On a single-page app, the page does not reload between views, so the data model persists. This makes the reset more important. Push each event from the code that handles the user action, for example the route change or the cart button handler. Do not push from a template that may render more than once.
Verifying the setup
Open GTM Preview mode and load the page. In the Tag Assistant summary, select the message for your event. Check that the trigger shows as matched and that the GA4 tag status is Fired. Open the Data Layer tab and confirm that the ecommerce object contains your items. Then check DebugView in GA4, covered in the next chapter, to confirm that the parameters arrive.
Exercise
A developer pushes { event: 'add_to_cart', ecommerce: { currency: 'USD', value: 24.99, items: [...] } } right after a view_item push that included a coupon. The GA4 tag fires correctly, but add_to_cart shows the coupon. What is wrong, and how do you fix it?
Show answer
The code skipped the reset. GTM merges pushes into its data model, so the old ecommerce.coupon value survived into the new event. Push { ecommerce: null } immediately before every e-commerce event, ideally through a shared helper such as pushEcommerce.
Exercise
You enabled Send Ecommerce data on a GA4 event tag with the event name {{Event}} and a Custom Event trigger on purchase. The tag fires, but GA4 shows no items. The push is dataLayer.push({ event: 'purchase', transaction_id: 'T_1', items: [...] }). What do you change?
Show answer
Nest the data under ecommerce: dataLayer.push({ event: 'purchase', ecommerce: { transaction_id: 'T_1', currency: 'USD', value: 49.98, items: [...] } }). The tag reads from ecommerce, so top-level items is ignored. Add the reset push before it and include currency and value.