product syntax on purchase Page | Community
Skip to main content
Level 4
February 19, 2023
Solved

product syntax on purchase Page

  • February 19, 2023
  • 2 replies
  • 1238 views

I have the below product string on purchase which currently works fine. I would like to add currency events and merchandising eVars. How can I add a sample event to the below logic?

 

I want to add event 1=2.3 and eVar25=”caps” for example

 

try { var productString = [], cleanProductString for (i=0; i<adobeDataLayer.length; i++) { if (adobeDataLayer[i].event == 'purchase') { var tmp = i; for (j=0; j < adobeDataLayer[tmp].transaction.orderItems.length; j++) { productString.push(";"+adobeDataLayer[tmp].transaction.orderItems[j].productSKU+";"+adobeDataLayer[tmp].transaction.orderItems[j].quantity+";"+adobeDataLayer[tmp].transaction.orderItems[j].price.toFixed(2)); } s.products = productString.toString(); } } } catch(e) {}

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Alexis_Cazes_

First of all please read this documentation: https://experienceleague.adobe.com/docs/analytics/implementation/vars/page-vars/products.html?lang=en

 

the syntax of s.products is

 

s.products = "category;product name;unit, total price; events;merchandisign eVars"

 

For merchandising eVars you need to first configure the eVar to have correct configuration. https://experienceleague.adobe.com/docs/analytics/implementation/vars/page-vars/evar-merchandising.html?lang=en

 

Make sure to choose the product syntax if you want to use as part of s.products

 

Based on your code you just need to add 2 more categories separated by a ";"

 

try { var productString = [], cleanProductString for (i = 0; i < adobeDataLayer.length; i++) { if (adobeDataLayer[i].event == 'purchase') { var tmp = i; for (j = 0; j < adobeDataLayer[tmp].transaction.orderItems.length; j++) { productString.push(";" + adobeDataLayer[tmp].transaction.orderItems[j].productSKU + ";" + adobeDataLayer[tmp].transaction.orderItems[j].quantity + ";" + adobeDataLayer[tmp].transaction.orderItems[j].price.toFixed(2) + ";" + "event1=2.3" + ";" + "eVar25=caps" ); } s.products = productString.toString(); } } } catch (e) {}

 

Please be aware that you can add more than one event and one more than one eVar but they will need to be separated by a pipe "|".

You can omit event but you will still to keep the placeholder for events using ";;"

 

Also the above logic is only good if event and eVar apply to each individual products, however if you want to use it at the global level like shipping price for all products and not for individual products then you should us the merchandising conversion syntax (you will need to configure evar to use this syntax).

 

Code will look as follow:

try { var productString = [], cleanProductString for (i = 0; i < adobeDataLayer.length; i++) { if (adobeDataLayer[i].event == 'purchase') { s.events += "event1=2.3" s.eVar25 = "caps" var tmp = i; for (j = 0; j < adobeDataLayer[tmp].transaction.orderItems.length; j++) { productString.push(";" + adobeDataLayer[tmp].transaction.orderItems[j].productSKU + ";" + adobeDataLayer[tmp].transaction.orderItems[j].quantity + ";" + adobeDataLayer[tmp].transaction.orderItems[j].price.toFixed(2) ); } s.products = productString.toString(); } } } catch (e) {}

2 replies

yuhuisg
Community Advisor
Community Advisor
February 20, 2023

Firstly, your code for traversing adobeDataLayer looks cumbersome and possibly incorrect (from an ACDL perspective). I suggest that you use adobeDataLayer's event listener: https://github.com/adobe/adobe-client-data-layer/wiki#addeventlistener

Back to your question:

You didn't mention if your eVar25 and event1 should be fixed to those values. But assuming that you're able to get the correct values, then you would set your s.products string according to the required syntax: https://experienceleague.adobe.com/docs/analytics/implementation/vars/page-vars/products.html?lang=en#s.products-in-appmeasurement-and-the-analytics-extension-custom-code-editor

Alexis_Cazes_
Alexis_Cazes_Accepted solution
Level 10
February 20, 2023

First of all please read this documentation: https://experienceleague.adobe.com/docs/analytics/implementation/vars/page-vars/products.html?lang=en

 

the syntax of s.products is

 

s.products = "category;product name;unit, total price; events;merchandisign eVars"

 

For merchandising eVars you need to first configure the eVar to have correct configuration. https://experienceleague.adobe.com/docs/analytics/implementation/vars/page-vars/evar-merchandising.html?lang=en

 

Make sure to choose the product syntax if you want to use as part of s.products

 

Based on your code you just need to add 2 more categories separated by a ";"

 

try { var productString = [], cleanProductString for (i = 0; i < adobeDataLayer.length; i++) { if (adobeDataLayer[i].event == 'purchase') { var tmp = i; for (j = 0; j < adobeDataLayer[tmp].transaction.orderItems.length; j++) { productString.push(";" + adobeDataLayer[tmp].transaction.orderItems[j].productSKU + ";" + adobeDataLayer[tmp].transaction.orderItems[j].quantity + ";" + adobeDataLayer[tmp].transaction.orderItems[j].price.toFixed(2) + ";" + "event1=2.3" + ";" + "eVar25=caps" ); } s.products = productString.toString(); } } } catch (e) {}

 

Please be aware that you can add more than one event and one more than one eVar but they will need to be separated by a pipe "|".

You can omit event but you will still to keep the placeholder for events using ";;"

 

Also the above logic is only good if event and eVar apply to each individual products, however if you want to use it at the global level like shipping price for all products and not for individual products then you should us the merchandising conversion syntax (you will need to configure evar to use this syntax).

 

Code will look as follow:

try { var productString = [], cleanProductString for (i = 0; i < adobeDataLayer.length; i++) { if (adobeDataLayer[i].event == 'purchase') { s.events += "event1=2.3" s.eVar25 = "caps" var tmp = i; for (j = 0; j < adobeDataLayer[tmp].transaction.orderItems.length; j++) { productString.push(";" + adobeDataLayer[tmp].transaction.orderItems[j].productSKU + ";" + adobeDataLayer[tmp].transaction.orderItems[j].quantity + ";" + adobeDataLayer[tmp].transaction.orderItems[j].price.toFixed(2) ); } s.products = productString.toString(); } } } catch (e) {}
xcode2295Author
Level 4
February 21, 2023

Thank you so much.. This is very helpful. This really helped me to get unstuck 🙂