Lesson 5 of 8
Reshaping Data For GA4
The most common real task: the site pushes some ecommerce data, but not in the shape GA4 documents. Rather than beg a developer to rewrite the dataLayer, adapt it in a Custom JS variable.
// The site gives you this:
// dataLayer.push({ event: 'order', products: [
// { sku: 'AUR-WH-09', title: 'Aurora Headphones', cost: '129.00', qty: 1 }
// ], order_total: '129.00' })
// Custom JS variable → GA4 'items'
function () {
var products = {{DLV - products}} || [];
return products.map(function (p) {
return {
item_id: p.sku,
item_name: p.title,
price: Number(p.cost),
quantity: p.qty,
};
});
}Then your GA4 purchase tag references this variable for items and a second one (Number({{DLV - order_total}})) for value. The site didn't change; you bridged the gap in GTM.
Key takeaway
When the dataLayer is close but not GA4-shaped, .map() it into the right structure inside a Custom JS variable. It is faster than a developer ticket and keeps the fix in your control.