See if this can be helpful:
dataLayer.push({
event: 'view_item',
ecommerce: {
currency: 'USD',
items: [{
item_id: 'SKU123', // What we're extracting
item_name: 'Cool Product',
price: 29.99
// ... other product data
}]
}
});
Once you create this variable then you should create a new Custom Javascript variable and use this code (in this example we want to fetch the item_id of the first product)
function() {
var ecomm = {{ecommerce}};
if (!ecomm || !ecomm.items || !ecomm.items.length) {
return undefined;
}
return ecomm.items[0].item_id || '';
}
In this way if you use GTM preview and select the DL Push which is supposed to have your eventModel and look into variables you should get the variable and not undefined
Let me know if this works, otherwise let's debug this, maybe you could share your eventModel DataLayer Push?
this is better explained in this article: https://blog.assertionhub.com/articles/extract-first-product-id-from-ga4-ecommerce-data-layer