title: Google Tag Manager custom javascript variable based on ecommerce datalayer object
body: <p>I'm trying to calculate the value of the shopping cart based on data contianed in the GTM datalayer ecommerce object. I'm bringing the entire ecommerce object into a datalayer variable so I can work with it in the custom js variable, but I'm unable to make the recursive calculation work.</p>
  
  <p>Here's an example of code from the datalayer:</p>
  
  <pre><code>{
    "event": "checkout",
    "ecommerce": {
      "checkout": {
        "products": [
          {
            "name": "coat",
            "id": "14M",
            "quantity": 1,
            "price": "100.00"
          },
          {
            "name": "pants",
            "id": "12L",
            "quantity": 3,
            "price": "50.00"
          }
        ]
      }
    }
  }`
  </code></pre>
  
  <p>My goal is to identifies the number of products in the product array and then perform the calculation of products price * product quantity as many times as is required to go through the whol product array. In this case, it would be something like this: </p>
  
  <p>(1*100) + (3*50) = 250.</p>
  
  <p>I've tried modifying the code from <a href="https://www.simoahava.com/analytics/measure-cart-value-in-enhanced-ecommerce/" rel="nofollow noreferrer">Simo Ahava's related blogpost</a> without success. The differences between his blogpost and my intention are:</p>
  
  <ol>
  <li>I don't need to set the cart value as an ecommerce Custom Metric. I want to just return the value of the cart in the custom js.</li>
  <li>I don't want this to involve adding or deleting items from the cart. Instead, I just want to calculate the value when the GTM Event "checkout" is triggered and calculate the value of the contained products.</li>
  </ol>
  
  <p>Thanks in advance.</p>