New datalayer variable in TMS for the ID of the reservation

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 :

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 :

// 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.

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.

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:

<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

<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

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

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

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

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. Once that’s done - please use the following method 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

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

Thanks Cédric,
The problem was that the command

      _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). 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

Please let me know if it helped.

1 Like