# Unable to fetch enhanced ecommerce dataLayer values

**URL:** https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455
**Category:** Data collection and tags
**Created:** [February 7, 2022, 11:06am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455 "2022-02-07T11:06:56Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [February 7, 2022, 11:06am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/1 "2022-02-07T11:06:56Z")

</div>

I’m trying to translate GTM/UA enhanced ecommerce dataLayer values to dataLayer variables to build a Piwik-compatible ecommerce transaction tag. I’ve added the dataLayer variables to the tag manager, but the values are still ‘undefined’.

This is the content of my GTM/UA dataLayer after a successful donation:

```auto
{
  "event": "donatie",
  "ecommerce": {
    "purchase": {
      "actionField": {
        "id": 8015,
        "revenue": 50,
        "list": "Gift",
        "option": "Ideal",
        "tax": "0",
        "step": 4,
        "shipping": 0
      },
      "products": [
        {
          "name": "Fonds noodzakelijke hulp",
          "id": "MOST",
          "price": 50,
          "brand": "",
          "category": "Fondsgift",
          "variant": "One-time",
          "quantity": 1
        }
      ]
    }
  }
}

```

I added these two dataLayer variables to the Tag Manager:

For the order\_id:

```auto
ecommerce.purchase.actionField.id

```

For the grand\_total:

```auto
ecommerce.purchase.actionField.revenue

```

In the debugger I see this:  
 ![Screenshot 2022-02-07 120354](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/422cf03ed2f732e13f9819fddbe6c7e75938a8d5.png)

The tranaction is loaded into the dataLayer when an event is firing.

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [February 7, 2022, 1:20pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/2 "2022-02-07T13:20:52Z")

</div>

Hi, thanks for reaching out. Unfortunately, it’s not possible to refer to nested attributes of a datalayer object directly. You could use a PPTM datalayer variable, but you would need to fetch the whole `ecommerce` object and then iterate through on the custom HTML tag level. It would look roughly like this:

```auto
    var orderDetails = {{ ecommerce object }};
    var order_id = orderDetails.purchase.actionField.id
    var grand_total = orderDetails.purchase.actionField.revenue
 

```

`{{ ecommerce object }}` is a PPTM datalayer variable `ecommerce`.

Please see [this thread](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/7) for complete code snippet examples for similar data layer objects.

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [February 7, 2022, 2:29pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/3 "2022-02-07T14:29:49Z")

</div>

Hi @pslonina, thanks for helping me out here. Really appreciate it! 😉 I got this far:

```auto
<script>
    var _paq = _paq || [];
    var orderDetails = {{ ecommerce object }};
    var order_id = orderDetails.purchase.actionField.id;
    var grand_total = orderDetails.purchase.actionField.revenue;
    var tax = orderDetails.purchase.actionField.tax;
    var products = orderDetails.purchase.products;
    var product.id = orderDetails.purchase.products.sku;
    var product.name = orderDetails.purchase.products.name;
    var product.category = orderDetails.purchase.products.category;
    var product.price = orderDetails.purchase.products.price;
    var product.quantity = orderDetails.purchase.products.quantity;
    
    products.forEach(function(product){
    _paq.push(["addEcommerceItem", product.id, product.name, product.category, product.price, product.quantity])
  	});
    _paq.push(["trackEcommerceOrder",order_id, grand_total]);
  </script>

```

But I think I’m missing something here, because this results in a JS error:

```auto
Uncaught SyntaxError: Unexpected token '.'

```

Any idea what I might be doing wrong here?

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [February 7, 2022, 3:16pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/4 "2022-02-07T15:16:38Z")

</div>

I’ve allowed myself to add some tweaks here and there:

```auto
<script>
    var _paq = _paq || [];
    var orderDetails = {{ ecommerce object }};
    var products = orderDetails.purchase.products;
    var order_id = orderDetails.purchase.actionField.id;
    var grand_total = orderDetails.purchase.actionField.revenue;
	var tax = parseInt(orderDetails.purchase.actionField.tax);
	var shipping = orderDetails.purchase.actionField.shipping;
  	

    products.forEach(function(product){
      _paq.push(["addEcommerceItem",product.id.toString(), product.name, product.category, product.price, product.quantity])
  
  });
    _paq.push(["trackEcommerceOrder",order_id, grand_total,, tax, shipping,]);
  </script>

```

Please note:

1. I’ve used `parseInt()` to change tax from string to number;
2. You don’t need to capture product attributes since we are operating on the whole products objects.

Please give it a try and let me know if it works 🙂

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [February 7, 2022, 3:29pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/5 "2022-02-07T15:29:48Z")

</div>

Great! Thanks, @pslonina! Works like a charm.

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [February 10, 2022, 12:05pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/6 "2022-02-10T12:05:55Z")

</div>

Additional question on what you said:

> [@pslonina](#):
>
> You don’t need to capture product attributes since we are operating on the whole products objects.

The GA enhanced ecommerce dataLayer contains more details about the transaction. On a product level there’s `brand` and `variant`, on purchase level there’s `list` and `option`. We currently use these variables in our reporting. Is there any way I could capture these and use them as custom dimensions?

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [February 11, 2022, 11:03am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/7 "2022-02-11T11:03:28Z")

</div>

Hi @michiel , most definitely!  
On the product level, you could utilize `Category` placeholder (please find more details [here](https://developers.piwik.pro/en/latest/data_collection/web/javascript_tracking_client/api.html#e-commerce)). I know that it’s not exactly the same as brand or variant, but it is an additional product-level attribute.

As for purchase level - custom dimensions in the scope of an event are the way to go. Please declare them first in Piwik PRO following [this article](https://help.piwik.pro/support/reports/custom-dimension/?pk_vid=0544a9abfaf0e0ae1644577365198009). Once that’s done - please use the following [method](https://developers.piwik.pro/en/latest/data_collection/web/javascript_tracking_client/api.html?highlight=setcustomdimensionvalue#custom-dimensions) to set custom dimension value:

`_paq.push(['setCustomDimensionValue', 1, 'dimensionValue']);`

Please make sure to set custom dimension values before sending the e-commerce event.

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [February 22, 2022, 10:36am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/8 "2022-02-22T10:36:48Z")

</div>

Hey @pslonina, I just ran into another challenge with ecommerce. We’re running a Woocommerce based webshop with the [WooCommerce Google Analytics](https://woocommerce.com/products/woocommerce-google-analytics/?quid=f959836a6220e09cde40823ec55b1c89) extension to track cart actions and purchases. I’m trying to translate this plugin’s dataLayer purchase event to Piwik, but I can’t get this to work. The dataLayer looks a bit different:

```auto
    {
        "0":"event",
        "1":"purchase",
        "2":{
            "transaction_id":"15780",
            "affiliation":"Compassion Cadeaus",
            "value":"90.00",
            "tax":"0",
            "shipping":"0",
            "currency":"EUR",
            "items":[
                {
                    "id":"14088",
                    "name":"Onderdak",
                    "category":"Noodhulp",
                    "price":"90",
                    "quantity":"1"
                }
            ]
        }
    }

```

I’m guessing the formatting declaring the event causes this? Is it possible to still use this dataLayer to make it work for Piwik PRO?

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [March 3, 2022, 3:29pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/9 "2022-03-03T15:29:13Z")

</div>

Hi @michiel ,  
My apologies for the late reply. You are right - the issue is the formatting and there is no simple way of addressing it from Piwik PRO level.

Is it possible to configure the plugin so it sends a key-value pair `"event": "purchase"`? If not - maybe there are alternative plugins that would build dataLayer objects for WooCommerce?

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [March 4, 2022, 11:04am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/10 "2022-03-04T11:04:54Z")

</div>

Hey @pslonina,  
I found another plugin ([GTM4WP](https://gtm4wp.com/)) that constructs dataLayer events in a correct way. I’m working on getting the `add_to_cart` event working, but I think I’m getting it wrong.

This is an example dataLayer for the `add_to_cart` event:

```auto
{
  "event": "add_to_cart",
  "ecommerce": {
    "currency": "EUR",
    "value": "8.95",
    "items": [
      {
        "item_id": "120730",
        "item_name": "Herbruikbare rietjes - Strawies®",
        "item_brand": "",
        "price": "8.95",
        "item_category": "Cadeautjes",
        "quantity": 1,
        "google_business_vertical": "retail",
        "id": "120730"
      }
    ]
  },
  "gtm.uniqueEventId": 24
}

```

And this is the tag I’m working with:

```auto
<script>
  var _paq = _paq || [];
  var orderDetails = {{ DLV | Ecommerce }}
  var products = orderDetails.items;
  
  products.forEach(function(product){
    _paq.push(['addEcommerceItem', product.id.toString(), product.name, product.category, product.price, product.quantity])
  });
</script>

```

Unfortunately, this results in a JavaScript error (`Uncaught ReferenceError: ecommerce is not defined`). I tried several other options, but none of them seems to work.

_EDIT_  
I changed the tag a bit and resolved the JS error, but the `add_to_cart` event still isn’t pushed to Piwik correctly.

Hope you can point me in the right direction!

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [March 7, 2022, 4:20pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/11 "2022-03-07T16:20:18Z")

</div>

Hi @michiel,  
Please use the following code:

```auto
<script>
    var _paq = _paq || [];
    var orderDetails = {{ ecommerce object }};
    var products = orderDetails.items;
    var grand_total = parseFloat(orderDetails.value);

    products.forEach(function(product){
      _paq.push(["addEcommerceItem",product.item_id, product.item_name, product.item_category, product.price, product.quantity])    
  	});
  
    _paq.push(["trackEcommerceCartUpdate", grand_total]);  
</script>

```

I’ve run a quick test and it looks good:

![image](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/eb5c75c766256915907b5eedbf0f5b160e9e89e0.png)

Please let me know if it worked for you too 🙂

Thanks!  
Piotr

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [March 8, 2022, 8:10am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/12 "2022-03-08T08:10:41Z")

</div>

Thanks, works like a charm! The only thing that doesn’t seem to work is updating the total cart value: adding a new item to the cart changes the value of the cart to the last added item:

 ![Screenshot 2022-03-08 083542](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/64e817cab373699b410b0470473f8c1ea76bb251.png)

I guess this has more to do with the `dataLayer`? The value in the `dataLayer` doesn’t calculate the sum of the price of the products added to the cart. Is there any way to make that calculation?

**EDIT**  
The `view_cart` event does update the total cart value:  
 ![Screenshot 2022-03-08 091831](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/5149e036f8d88fee3be5b0f88b508e604cebee74.png)

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [March 10, 2022, 8:41am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/13 "2022-03-10T08:41:16Z")

</div>

Hi @michiel,  
please use this script instead of the last one:

```auto
<script>
    var _paq = _paq || [];
    var orderDetails = {{ ecommerce object }};
    var products = orderDetails.items;
	var total_price = 0
  	var prices = []
	products.forEach(function(product){
    	prices.push((product.price*product.quantity).toFixed(2));
	});

   for(var i=0; i < prices.length; i++){
        total_price += prices[i];  	
	}   
   
    products.forEach(function(product){
      _paq.push(["addEcommerceItem",product.item_id, product.item_name, product.item_category, product.price, product.quantity])
       
  });
  
       _paq.push(["trackEcommerceCartUpdate", total_price.toString()]);

  </script>

```

It will calculate the total value based on the prices of the items.

Please let me know if that helped.  
Thanks,  
Piotr

**EDIT:** I’ve tweaked the code above a bit.

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [March 11, 2022, 3:59pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/14 "2022-03-11T15:59:21Z")

</div>

Thanks for your help, @pslonina!

It’s strange, but I get mixed results with the `add_to_cart` event.

**[Product overview page](https://shop.compassion.nl/webshop/)**  
On the product overview page I can add a single product if the product doesn’t have any options to choose from. If I add such a product to the cart, I get a Cart update event in the tracker debugger with the correct product, quantity and price. From the overview page, I can only add 1 item to the cart.

_Tracker debugger_  
 ![Screenshot 2022-03-11 164704](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/76ac1c7ec40c73676b4332e46ba3f1d32e80f2df.png)

_Tag manager debugger `add_to_cart` event_

 ![Screenshot 2022-03-11 165601](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/2deaf6850227350e93c889f98cd5ae7f1f79d54a.png)

**[Product detail page](https://shop.compassion.nl/product/compassion-tonys-chocoladereep/)**  
If I add a product to the cart from a product detail page, I can see the `add_to_cart` tag fires, but it doesn’t register an event in the tracker debugger. I tested this with ánd without the option to prevent page reload in the tag manager debug mode.

In the tag manager debugger I still see the `add_to_cart` event, including the right details for the cart update. In the example below I added 4 chocolate bars to the cart with a total value of 26 EUR, which add the following to the dataLayer:

 ![Screenshot 2022-03-11 165242](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/d846054025b7ac85563f5ff0483f494c621f893b.png)

I’m kinda lost here… Any suggestions? 😉

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [March 14, 2022, 12:43pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/15 "2022-03-14T12:43:31Z")

</div>

Hi @michiel,  
Is it possible to take a look at the actual website?

Thanks,  
Piotr

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [March 14, 2022, 1:00pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/16 "2022-03-14T13:00:20Z")

</div>

Hi @pslonina, sure! I linked the pages in the ‘subtitles’ of my last comment, but that was not really obvious… 😉

You can find [the webshop here](https://shop.compassion.nl/).

---

<div class="post-metadata">

### Author: ![pslonina](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/pslonina/32/1711_2.png) [@pslonina](https://community.piwik.pro/u/pslonina)
#### Post date: [March 14, 2022, 2:21pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/17 "2022-03-14T14:21:15Z")

</div>

Oh, it was right there for the whole time 😃 Thanks!

I was able to pinpoint the problem. So the thing is that for **Product overview page** , product quantities are numbers while for **Product detail page** product quantities are string. Therefore you need to use `parseFloat()` in order to convert them to numbers. Please see the code below:

```auto
<script>
  var _paq = _paq || [];
  var orderDetails = {{ DLV | Ecommerce }};
  var products = orderDetails.items;
  var grand_total = parseFloat(orderDetails.value);
  
  products.forEach(function(product){
    _paq.push(["addEcommerceItem", product.item_id, product.item_name, product.item_category, product.price, parseFloat(product.quantity)])    
  	  });
    _paq.push(["trackEcommerceCartUpdate", grand_total]);
</script>

```

Please let me know did it go 😉

Best,  
Piotr

---

<div class="post-metadata">

### Author: ![michiel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/michiel/32/236_2.png) [@michiel](https://community.piwik.pro/u/michiel)
#### Post date: [March 14, 2022, 2:37pm UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/18 "2022-03-14T14:37:51Z")

</div>

Yes, this works! Thanks!

---

<div class="post-metadata">

### Author: ![kuba](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/kuba/32/7_2.png) [@kuba](https://community.piwik.pro/u/kuba)
#### Post date: [April 7, 2022, 10:24am UTC](https://community.piwik.pro/t/unable-to-fetch-enhanced-ecommerce-datalayer-values/455/19 "2022-04-07T10:24:29Z")

</div>

A post was split to a new topic: [Product scoped custom dimensions](https://community.piwik.pro/t/product-scoped-custom-dimensions/564)
