google tag manager template only reading first elements of array for events with more than 1 item

453 Views Asked by At

I'm implementing this google tag manager template(GA3/UA) but the issue is that when there is more than 1 product, it only catches or reads the first element of the arrays, this happens for all the events that can have more than 1 product, I'm kinda new to this, can anyone guide me in why is this happening? here is the code for the sandbox javascript, I just need to get all the elements of the arrays, not the first one, everything else works fine. Thank you very much in advance.

___SANDBOXED_JS_FOR_WEB_TEMPLATE___

const log = require('logToConsole');
const makeTableMap = require('makeTableMap');
// Call APIs to set dataLayer and copy values from dataLayer
const copyFromDataLayer = require('copyFromDataLayer');
// Set the function where we want to push the dataLayer
const createQueue = require('createQueue');
const dataLayerPush = createQueue('dataLayer');

//Validate structure
var ecommerce_data = copyFromDataLayer('ecommerce.'+data.activity);
if(ecommerce_data == undefined){
  //dataLayerPush({'event': "error", "info": "enhanced ecommerce base structure not found for "+ data.activity});
 // return;
  ecommerce_data = {};
}
var ecommerce_actionField = copyFromDataLayer('ecommerce.'+data.activity+".actionField");
if(ecommerce_actionField == undefined){ ecommerce_actionField = {};}
var ecommerce_products = copyFromDataLayer('ecommerce.'+data.activity+".products");
if(ecommerce_products == undefined){ecommerce_products = []; ecommerce_products.push({});}
var ecommerce_impressions = copyFromDataLayer('ecommerce.'+data.activity);
if(ecommerce_impressions == undefined){ecommerce_impressions = []; ecommerce_impressions.push({});}
var ecommerce_promotions = copyFromDataLayer('ecommerce.'+data.activity+".promotions");
if(ecommerce_promotions == undefined){ecommerce_promotions = []; ecommerce_promotions.push({});}


//this fucntion works only for one element edition. 
//Usable for: actionFiel, product click, detail, add, remove, promotionClick
//Params: aField, ecommerce_origin
function merge_element() {
  log("entro 1");
  let key;
  for (key in arguments[0]) {
      arguments[1][key] = arguments[0][key];
    }
  return arguments[1];
}
//this fucntion works for a list of elements edition. 
//Usable for: products, impressions and promotions
//Params: prods, ecommerce_origin, type_index
function merge_elements(){
  if(arguments[2] == "all"){
    let key, elem;
    for(elem in arguments[1]){
       for (key in arguments[0]) {
         arguments[1][elem][key] = arguments[0][key];
        }
    }
  }else{
    var indexes = data.index.split(",");
    log("indexes", indexes);
    let elem, key;
    for(elem in indexes){
      if(!arguments[1].hasOwnProperty(indexes[elem]-1)){
        log(indexes[elem] + " is not a number or not beyong your array size");
        continue;
      }
      for (key in arguments[0]) {
         arguments[1][indexes[elem]-1][key] =arguments[0][key];
        }
    } 
  }
  return arguments[1];
}

var aField = {}, prods = {}, impr = {},prom ={};
if(data.actionField != undefined){aField = makeTableMap(data.actionField, 'key', 'value');}
if(data.products != undefined){prods = makeTableMap(data.products, 'key', 'value');}
if(data.impressions != undefined){impr = makeTableMap(data.impressions, 'key', 'value');}
if(data.promotions != undefined){prom = makeTableMap(data.promotions, 'key', 'value');}
var type_index = "all";
if(data.type_index != undefined){type_index = data.type_index;}
const datalayer = {};
datalayer[data.activity] ={};

datalayer.currencyCode = copyFromDataLayer('ecommerce.currencyCode') != undefined? copyFromDataLayer('ecommerce.currencyCode'): data.currencyCode;


if(data.activity == "checkout"||data.activity =="click"||data.activity =="detail"|| data.activity == "purchase"){
  log("entro click");
    datalayer[data.activity].actionField = merge_element(aField, ecommerce_actionField);
    datalayer[data.activity].products = merge_elements(prods, ecommerce_products, type_index);
}

if(data.activity == "add"||data.activity == "remove"){
    datalayer[data.activity].products = merge_elements(prods, ecommerce_products, type_index);
}
if(data.activity == "promoView"|| data.activity == "promoClick"){
    datalayer[data.activity].promotions = merge_elements(prom, ecommerce_promotions, type_index);
}
if(data.activity == "impressions"){
    datalayer[data.activity].impressions = merge_elements(impr, ecommerce_impressions, type_index);
}

//basic data recompilation
var event = "", eventCategory = "",eventAction = "",eventLabel = "",eventValue = 0;
if(data.basic_data){
  event = data.event_name +"";
  eventCategory = data.event_category+"";
  eventAction = data.event_action+"";
  eventLabel = data.event_label+"";
  eventValue = data.event_value+"";
}
if(event == ""){
  event = copyFromDataLayer('event')+ "_changed";
}if(eventCategory == ""){
  eventCategory = copyFromDataLayer('eventCategory') != null? copyFromDataLayer('eventCategory'): "Enhanced ecommerce";
}if(eventAction == ""){
  eventAction = copyFromDataLayer('eventAction') != null? copyFromDataLayer('eventAction'): data.activity;
  
}if(eventLabel == ""){
  if(copyFromDataLayer('eventLabel') != null){
    eventLabel= copyFromDataLayer('eventLabel');
  }else{
    if(data.activity == "impressions"){
      eventLabel = datalayer[data.activity].impressions[0].name;
    }
    if(data.activity == "promoView"|| data.activity == "promoClick"){
    eventLabel = datalayer[data.activity].promotions[0].name;
    }else{
      eventLabel=datalayer[data.activity].products[0].name;
    }
  }
}if(eventValue == 0){
    if(copyFromDataLayer('eventValue') != null){
      eventValue = copyFromDataLayer('eventValue');
    }
  }

dataLayerPush({'event': event,'eventCategory': eventCategory, 'eventAction': eventAction, 'eventLabel': eventLabel, 'eventValue': eventValue,'ecommerce':datalayer});

data.gtmOnSuccess();
1

There are 1 best solutions below

0
BNazaruk On

You can Mock the DL values in the template unit tests.

Mock it properly, run the tests, debug the values. If that is not enough to make the issue obvious, post a mcve of your attempt.

Don't just dump the whole template in a question. Show your debugging attempts.