Expand my Community achievements bar.

SOLVED

Pass entire array option in productListItem array

Avatar

Level 2

During analytics implementation using websdk I am getting a product array using ACDL on website for commerce events and want to fetch those value in launch and pass it as entire array in xdm data element mapping. Also, need to pass some of the values as evars and events in product string. What should be the best practice or sample code to traverse through the array and create an object that can be directly mapped as array. 

sample datalayer code:

 

window.adobeDataLayer.push({
product: {
"productListItems": [
{
"Qty": 18,
"brand": "ABC",
"category": "DEF",
"StarRating": 4.4792,
"sku": "12345",
"stockStatus": "in stock",
}

{
"Qty": 20,
"brand": "ABCD",
"category": "GHIJ",
"StarRating": 4.4,
"sku": "12345",
"stockStatus": "in stock",
}
]
}
});

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hey @ChiragMa 

 

You can grab the productlistitems array from the datalayer and put it into another array using the code below. This script can be used in custom code of a data element (let's call it prodList) within Launch.

 

// Access the productListItems array from the object
const productListItems = window.adobeDataLayer[0].product.productListItems;

// Initialize an array to store the extracted data
const extractedData = [];

// Check if productListItems is an array and not empty
if (Array.isArray(productListItems) && productListItems.length > 0) {
  // Loop through the productListItems array and extract data
  productListItems.forEach((item, index) => {
    const extractedItem = {
      index: index + 1,
      Qty: item.Qty,
      brand: item.brand,
      category: item.category,
      StarRating: item.StarRating,
      sku: item.sku,
      stockStatus: item.stockStatus,
    };
    
    // Push the extracted data into the new array
    extractedData.push(extractedItem);
  });
} else {
  console.log("productListItems is either not an array or is empty.");
}

// Now you have the extracted data in the `extractedData` array
return extractedData;

 Once you have the above array of productlistitems, you can then map this to another data element of type, XDM Object. Once you specify the schema in the data element, you would map the prodList data element from above in the productListItem.

abhinavbalooni_0-1701341707872.png

Please ensure you are sticking to the values you are allowed in the product list items field group. You can refer to the below: https://github.com/adobe/xdm/blob/master/docs/reference/datatypes/productlistitem.schema.json

Once you've done that, you would also need to update the commerce object for the specific ecommerce event you are passing the information for.

 

As for the merchandising eVars, you would need to use the Adobe Analytics Experience Event Template field group. Within that field group, you have a productlistitems array which has eVars in it. Populating eVars within this array object would function similar to merch eVars in AA.

abhinavbalooni_1-1701341967057.png

 

Here's a link to detailed information around this field group.

 

https://experienceleague.adobe.com/docs/analytics/implementation/aep-edge/variable-mapping.html?lang...

 

Hope the above should give you a start.

 

Reach out if you need any further information around it.

 

Cheers,

Abhinav

 

View solution in original post

13 Replies

Avatar

Correct answer by
Community Advisor

Hey @ChiragMa 

 

You can grab the productlistitems array from the datalayer and put it into another array using the code below. This script can be used in custom code of a data element (let's call it prodList) within Launch.

 

// Access the productListItems array from the object
const productListItems = window.adobeDataLayer[0].product.productListItems;

// Initialize an array to store the extracted data
const extractedData = [];

// Check if productListItems is an array and not empty
if (Array.isArray(productListItems) && productListItems.length > 0) {
  // Loop through the productListItems array and extract data
  productListItems.forEach((item, index) => {
    const extractedItem = {
      index: index + 1,
      Qty: item.Qty,
      brand: item.brand,
      category: item.category,
      StarRating: item.StarRating,
      sku: item.sku,
      stockStatus: item.stockStatus,
    };
    
    // Push the extracted data into the new array
    extractedData.push(extractedItem);
  });
} else {
  console.log("productListItems is either not an array or is empty.");
}

// Now you have the extracted data in the `extractedData` array
return extractedData;

 Once you have the above array of productlistitems, you can then map this to another data element of type, XDM Object. Once you specify the schema in the data element, you would map the prodList data element from above in the productListItem.

abhinavbalooni_0-1701341707872.png

Please ensure you are sticking to the values you are allowed in the product list items field group. You can refer to the below: https://github.com/adobe/xdm/blob/master/docs/reference/datatypes/productlistitem.schema.json

Once you've done that, you would also need to update the commerce object for the specific ecommerce event you are passing the information for.

 

As for the merchandising eVars, you would need to use the Adobe Analytics Experience Event Template field group. Within that field group, you have a productlistitems array which has eVars in it. Populating eVars within this array object would function similar to merch eVars in AA.

abhinavbalooni_1-1701341967057.png

 

Here's a link to detailed information around this field group.

 

https://experienceleague.adobe.com/docs/analytics/implementation/aep-edge/variable-mapping.html?lang...

 

Hope the above should give you a start.

 

Reach out if you need any further information around it.

 

Cheers,

Abhinav

 

Avatar

Level 2

Thanks Abhinav this is very helpful.

I tried to create a similar code by taking additional evars into consideration and designed an array to match productListItem array structure. Can you see below code and confirm if this works and merch evars will get mapped properly. Also, is it can we manipulate product string in report suite if we pass data in custom field group and fields rather than using auto mapped variables.

 

var product = adobedatalayer.product.productItems;
var productItem = [];
product.forEach(function(item, index, array){
productItem.push({
"SKU": item.sku,
"name":item.title,
"quantity":qty,
"priceTotal":price,
//"_experiance.analytics.customdimensions.eVars.eVar1" : item.category
"_experience": {
"analytics": {
"customDimensions" : {
"eVars" : {
"eVar1" : item.category,
"eVar2" : item.subCategory
}
},
"event1to100" : {
"event4" : {
"value" : item.unitPrice
},
"event10" : {
"value" : item.originalPrice,

}
}
}}


});
});
return productItem;

Avatar

Community Advisor

Hey @ChiragMa 

 

The code looks good. Did you give it a shot ? Should work based on what I can see. As for the modifications in the report suite, what do you mean by that ? 

 

Also, as of now, I think you would have to use the automatic mapping for the merch eVars if you are going the web SDK way.

 

Cheers.

Avatar

Level 2

I am doing it websdk way but by creating custom field group with custom field names in the product array and then using processing rules to map product details using processing rules. But not sure if it's going to work or not as we are currently in implementation phase.

 

Avatar

Community Advisor

Might work. Try it in a dev sandbox and see how it goes. Or else, try the web sdk approach, make sure it works for you and then can try the custom approach. Any reason to go the custom way cause you are adding complexity by introducing processing rules in the scheme of things?

 

Cheers

Avatar

Level 2

Not really, preffered approach is to leverage experience event schema just trying to see if the other way also works.

Avatar

Community Advisor

Cool. Do update if you end up making an alternate approach work. 

 

Cheers !

Avatar

Level 3

Hi @ChiragMa ,

 

Try below code, i'm using this code for one of my client and it is working fine on Production, use this code in Data Element and pass that data element to productList array data element.


var products = _satellite.getVar("product");

for (var i = 0; i < products.length; i++) {
var product = products[i];
var name = product.productName;
var sku = product.productVariantId;
var quantity = product.productQuantity;
var priceTotal = product.productSalePrice;
var price = product.productPrice;
var category = product.productCategory;
var discount = product.discountPercentage;
var size = product.productSize;
var stock = product.productInStock;
var id = product.productId;
var discountedPrice = product.productDiscountedPrice;

var item = {
name: sku,
quantity: "",
priceTotal: "",

_experience: {
analytics: {
customDimensions: {
eVars: {
eVar13: name,
eVar14: quantity,
eVar15: id,
eVar16: size,
eVar17: priceTotal,
eVar18: category,
eVar37: price,
eVar38: discount,
eVar39: stock,
eVar52: sku,
eVar53: discountedPrice
},
},
},
},
};

result.push(item);
}

Avatar

Level 2

Thank you for response.

All the evars sent in product string under productListItems[]  needs to be enabled as merchandising evars with product syntax selected in analytics report suite?

Avatar

Community Advisor

Hey @ChiragMa 
Yes that needs to be done in the admin report suite settings.

Avatar

Level 2

Hi

I want to confirm that for mapping productListItems[].priceTotal in case of multiple quantity purchased by user for same product should we pass single unit price and AA will multiply quantity to give total revenue or we need to pass total price directly(single unit price*quantity).

Avatar

Level 2

Are you storing values like price or discounted price in counter evars or string. Also, is there any harm in storing these numeric values as an event.

Avatar

Level 3

Hi Chirag,

 

Their is not harm to store values in numeric format, only for 0 value it will not show anything in Adobe Analytics report if you store in numeric.