3
votes

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?

1
Is the window.datalayer variable even available when you use it? What is the rule/condition for the tag?Blexy
Blexy: Trigger could be 'event' : 'purchase'; tag is firing correctly, but ecommerce object via macro is not available. I am either not sure, if macros are capable to pass JS object.Jakub Kriz

1 Answers

1
votes

Google Tag Manager macro/variable

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;
  }
}