Reputation: 1529
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
}]
}
}
}
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.
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.
When I use in custom TAG this code:
var eecom = {{ECOMMERCE OBJECT}}
console.dir(eecom);
It returns empty string.
Is there any common solution to retrieve Enhanced Ecommerce object when it appears in dataLayer?
Upvotes: 3
Views: 821
Reputation: 1529
In GTM JavaScript macro is not capable to return a JS object. Niether JS variable is not capable to return JS object.
So finally I build this macro into the TAG and it works. Final solution looks like this:
var dl = window.dataLayer;
var eecom = "";
var al = dl.length;
for (var i = 0; i < al; i++) {
if(dl[i].ecommerce){
eecom = dl[i].ecommerce;
break;
}
}
Upvotes: 1