# New datalayer variable in TMS for the ID of the reservation

**URL:** <https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445>\
**Category:** Data collection and tags\
**Created:** [January 31, 2022, 2:57pm UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445 "2022-01-31T14:57:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cedric.duma](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/cedric.duma/32/194_2.png) [@cedric.duma](https://community.piwik.pro/u/cedric.duma)\
**Post date:** [January 31, 2022, 2:57pm UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/1 "2022-01-31T14:57:13Z")

</div>

I have a question related to datalayer.  
I currently have a datalayer builded for the Google solution (GTM+Analytics). I track ecommerce orders (but not only).  
So my datalayer for a purchase is something like :

```auto
dataLayer.push({
  'event' : 'Reservation',
  'P_Category ' : %PageCategory,   
  'P_Destination' : %Destination,    
  'P_Cible ' : %Cible,             
  'P_Saison' : %Saison,             
  'P_Affinites' : %Affinites, 
  'dimension6': %ProduitCategorie,
  'dimension7': %ProduitSousCategorie,
  'dimension9': %CaracteristiqueOffre,  
  'dimension10': %Departement,  
  'dimension11': %Partenaire 
});
  'ecommerce': {
    'purchase': {
      'actionField': {
        'id': %IdReservation, // **I want to use this as a variable**                  
        'revenue': %TotalReservation                     
      },
      'products': [{                            
        'name': %TitreOffre,    
        'id': %IdOffre,
        'price': %PrixOffre,     
        'quantity': 1,
        'brand': %Prestataire,
        'category': %ProduitCategorie,
        'variant': %ProduitSousCategorie,
        'dimension1': %PageCategory,
        'dimension2': %Destination,
        'dimension3': %Cible,
        'dimension4': %Saison, 
        'dimension5': %Affinites
       }]
    }
  }
});

```

My question: is it possible to create a new datalayer variable in TMS for the ID of the reservation. As you can see **it’s ecommerce.purchase.actionField.id**  
The question behind all of this: do we really need to completly rewrite the datalayer push to something like :

```auto
// register all purchased items

_paq.push(["addEcommerceItem",
    "66251929", // SKU
    "Red Unicorn Coffee Mug", // name
    "Tableware", // category
    8.00, // price
    1 // quantity
]);

_paq.push(["addEcommerceItem",
    "08273511", // SKU
    "SUPER Blue Ink Pen 0.2", // name
    "Office products", // category
    2.00, // price
    2 // quantity
]);

// track order
_paq.push(["trackEcommerceOrder",
    "online-5289", // ID
    16.00, // grand total (value + tax + discount + shipping)
    10.00, // sub total (value + tax + discount)
    1.00, // tax
    6.00, // shipping
    2.00 // discount
]);

```

Thank you for your feedback

Cédric.

---

<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 1, 2022, 10:59am UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/2 "2022-02-01T10:59:31Z")

</div>

Hi Cédric, thanks for reaching out!

The recommended approach for both - datalayer events structure and for the e-commerce tracking PPTM tags is described [In this thread](https://community.piwik.pro/t/how-to-set-up-ecommerce-tracking/79/2).

That said - it’s obviously not the only way. 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. Unfortunately, it’s not possible to refer to nested attributes of a datalayer object directly. It would look roughly like this:

**For cart update:**

```auto
<script>
    var _paq = _paq || [];
    var orderDetails = {{ ecommerce object }};
    var products = orderDetails.purchase.products;
    var cart_amount = orderDetails.purchase.actionField.revenue;

    products.forEach(function (product) {
        _paq.push(["addEcommerceItem", product.id, product.name, product.category, product.price, product.quantity])
    });
    _paq.push(["trackEcommerceCartUpdate", cart_amount]);
</script>

```

**For order confirmation**

```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;

    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>

```

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

I hope the above helps!  
Thanks,  
Piotr

---

<div class="post-metadata">

**Author:** ![cedric.duma](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/cedric.duma/32/194_2.png) [@cedric.duma](https://community.piwik.pro/u/cedric.duma)\
**Post date:** [February 3, 2022, 3:52pm UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/3 "2022-02-03T15:52:29Z")

</div>

Thank you Piotr for your reply.  
i will test it :

- create a new datalayer variable with the name of the ecommece object in the datalayer push installed initially for Google.
- creatre a new tag in PPTMS called for example “order confirmation”. Type of tag: asynchron custom code.
- paste your code, replace {{ecommerce}} with my new created DL variable.

And with that, it will populate my ecommerce dashboard. correct?

I have one more question: as you can see in my datalayer push, inside the ecommerce objet for a product i have all the usual variables (id, price, quantity, …) and i also have some custom dimensions. Is that possible to use them “inside the ecommerce scope” ? To create some point of view for ecommerce results (ex: get ecommerce performance by destination)

Thank you,

Cédric

---

<div class="post-metadata">

**Author:** ![cedric.duma](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/cedric.duma/32/194_2.png) [@cedric.duma](https://community.piwik.pro/u/cedric.duma)\
**Post date:** [February 4, 2022, 9:23am UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/4 "2022-02-04T09:23:08Z")

</div>

Hi again Piotr,

In the loop products.forEach(function(product){}  
I think I have an issue. In the google ecommerce DataLayer  
we have this :  
ecommerce → purchase → products → 0 → first product attribute  
If we have more than 1 product inside the order, the 0 will change to 1,2, etc.  
When I debug the order confirmation i have 0 product in the ecommerce event:  
 ![image](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/1X/8a33acf151676a8192b4304dff209a10601fa0dc.png)

I could use for the products var : orderDetails.purchase.products.0 because usually users can only buy 1 product only. But I would like to make it works perfectly and also if for any reason the user can order more than 1 product.

Could you give me a tip?

Thanks  
Cédric

---

<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 4, 2022, 1:05pm UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/5 "2022-02-04T13:05:54Z")

</div>

Hi Cédric, please find my feedback below:

> inside the ecommerce objet for a product i have all the usual variables (id, price, quantity, …) and i also have some custom dimensions. Is that possible to use them “inside the ecommerce scope” ? To create some point of view for ecommerce results (ex: get ecommerce performance by destination)

Yeah, please use Event scope custom dimensions. Please declare them first in Piwik PRO following [this article](https://help.piwik.pro/support/reports/custom-dimension/). 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 value before sending the e-commerce event.

> I could use for the products var : orderDetails.purchase.products.0 because usually users can only buy 1 product only. But I would like to make it works perfectly and also if for any reason the user can order more than 1 product.

Please provide me with the data layer object you are working on (the actual object, not the definition) - I’ll push it locally and test everything.

Thanks!  
Piotr

---

<div class="post-metadata">

**Author:** ![cedric.duma](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/cedric.duma/32/194_2.png) [@cedric.duma](https://community.piwik.pro/u/cedric.duma)\
**Post date:** [February 4, 2022, 3:29pm UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/6 "2022-02-04T15:29:07Z")

</div>

```auto
event:"Reservation"
ecommerce:Object
         purchase:Object
               actionField:Object
                    id:"15258-1643988306.426"
                    revenue:"120"
               products:Array[1]
                    0:Object
                         name:"Coffret Cadeau, Repas au fil de l&rsquo;eau"
                         id:15258
                         price:"120"
                         brand:null
                         dimension1:"Activités"
                         dimension2:"Lorraine"
                         dimension3:"Best Agers"
                         dimension4:"Toutes saisons"
                         dimension5:"La culture et le patrimoine"
                         category:"Activités"
                         variant:"Autour de l'eau"
                         quantity:1

```

---

<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 5, 2022, 9:11am UTC](https://community.piwik.pro/t/new-datalayer-variable-in-tms-for-the-id-of-the-reservation/445/7 "2022-02-05T09:11:01Z")

</div>

Thanks Cédric,  
The problem was that the command

```auto
      _paq.push(["addEcommerceItem",product.id, product.name, product.category, product.price, product.quantity])

```

wouldn’t work with a SKU that is a number (please see the documentation [here](https://developers.piwik.pro/en/latest/data_collection/web/javascript_tracking_client/api.html?highlight=addecommerceitem#e-commerce)). It needs to be stringified. You can either make sure that it’s pushed as a string or use `toString()` method to convert the product.id to a string. Once the product.id is stringified, everything works:

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

Please let me know if it helped.
