Situation
Enhanced Ecommerce tracking is fully implemented on some website. This is done by build-in feature Enhanced Ecommerce - use dataLayer.
Example:
var ecommercObject = {
'ecommerce': {
'purchase': {
'actionField': {
'id': 'T12345', // Transaction ID. Required for purchases and refunds.
'affiliation': 'Online Store',
'revenue': '35.43', // Total transaction value (incl. tax and shipping)
'tax':'4.90',
'shipping': '5.99',
'coupon': 'SUMMER_SALE'
},
'products': [{ // List of productFieldObjects.
'name': 'Triblend Android T-Shirt', // Name or ID is required.
'id': '12345',
'price': '15.25',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Gray',
'quantity': 1,
'coupon': '' // Optional fields may be omitted or set to empty string.
},
{
'name': 'Donut Friday Scented T-Shirt',
'id': '67890',
'price': '33.75',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'quantity': 1
}]
}
}
}
Desired behavior
I want to re-use Enhanced Ecommerce implementation for another analytics system.
So i need macro/variable in GTM, that returns Enahnced Ecommerce JavaScritp Object.
Current attempts
Custom JS: ECOMMERCE OBJECT
I tried in console this macro that works, but not in GTM.
(function(){
myStringArray = window.dataLayer;
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
if(myStringArray[i].ecommerce){
return myStringArray[i];
}
}
return false;
})()
DataLayer variable: ECOMMERCE OBJECT
I either tried Macro type DataLayer with dataLayer variable name "ecommerce". No effect.
Problem
When I use in custom TAG this code:
var eecom = {{ECOMMERCE OBJECT}}
console.dir(eecom);
It returns empty string.
Question
Is there any common solution to retrieve Enhanced Ecommerce object when it appears in dataLayer?
window.datalayer
variable even available when you use it? What is the rule/condition for the tag? – Blexy