javascriptgoogle-analyticse-commercedata-layers

Data Layer Quotations Removal


I am trying to dynamically fill my ecommerce data layer for Google Analytics. However some information such as the 'transactionTotal' and 'transactionTax' are not passing through because they are currently surrounded by double quotations and those need to be removed to pass the information to GA.

I tried .replace(), i am just unsure where to put it as it does not work when place in the function

<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
   'event': 'orderCompleted',
   'transactionId': '{{TransactionTotal}}',
   'transactionAffiliation': 'Blank Company',
   'transactionTotal': '{{TransactionTotal}}',
   'transactionTax': '{{TransactionTax}}',
   'transactionShipping': '{{TransactionShipping}}',
   'transactionProducts': [{

   },{

   }]

});  

</script>

function () {
     try {
       return document.querySelector('.summarySubtotal>span').innerText.trim().replace(/\$|,/g, '');
   }catch (e) {
  return "";

}
} 

in the data layer the information should be

from:

transactionTotal: "700.00"

to:

transactionTotal: 700.00


Solution

  • the quotes in your tag around TransactionTotal variable are redundant, jsut use

    'transactionTotal': {{TransactionTotal}},