google-analyticsenhanced-ecommerce

Enhanced ecommerce don't recognize internal promotion view object


I've been implementing all the enhanced eCommerce tracks for the past few weeks and I could do most of the job successfully thanks to Simo Ahava's blog. But now I'm struggling with the internal promotion view tracking.

I choose to implement the view tracking with the concept of True View Impressions also with a base on Simo's work and for products it was ok. So I modified the customTasks from the link to track internal promotion but, for some reason, the enhanced eCommerce isn't recognizing the promoView object. But it's recognizing the promoClick (?).

I've made a test: I substitute the promoClick for a impression object and it works! So, my strong guest, it's that the problem it's really on my object. My object's format can be seen here.

And to illustrate the way the object it's being constructed:

      var targetElement = {{Click Element}},
      event         = {{Event}},
      batch         = window[promoBatchVariableName],
      impressions   = google_tag_manager[{{Container ID}}].dataLayer.get('ecommerce.promoView.promotions'),
      ecomObj       = { };

  if (event === 'gtm.click') {
    while (!targetElement.getAttribute(promoIdAttribute) && targetElement.tagName !== 'BODY') {
      targetElement = targetElement.parentElement;
    }
  }

  var latestPromoImpression = impressions.filter(function(impression) {
    return impression.id === targetElement.getAttribute(promoIdAttribute);
  }).shift();

  var promoImpressionsArr = batch.map(function(id) {
    return impressions.filter(function(impression) {
      return impression.id === id;      
    }).shift();
  });

  if (event === 'gtm.elementVisibility'){
    promoImpressionsArr[maxPromoBatch - 1] = latestPromoImpression;
  }

  console.log(ecomObj)
  ecomObj.promoView = { promotions: promoImpressionsArr};

  if (event === 'gtm.click') {
    ecomObj.promoClick = {
      promotions: [latestPromoImpression]
    };
    console.log("click")
  }

  return {
    ecommerce: ecomObj
  };
}

Could someone help me with some ideas?


Solution

  • This answer is just to close the question! As I pointed in the comments:

    " I found the problem. And it's not on my object itself only. xD The problem is the undefined elements as you pointed at the beginning of our talk. I'm waiting for the dev team to change the data-attributes of the elements on our site's pages because sometimes we don't get any individual identifier variable. So, in the meantime, I've implemented a way to get always a product id even in these cases but as the identifier doesn't exist in the CSS selector if the element has an id in the 'entrance object', the element is set as undefined. "