Expand my Community achievements bar.

Help shape the future of AI assistance by participating in this quick card sorting activity. Your input will help create a more effective system that better serves your needs and those of your colleagues.

Capture all Array data in event

Avatar

Level 1

For capturing data that is set in Array in JS data layer and match that with the product information of my orders I need to write a custom code. But I can't figure out what custom code I can use.

In analytics I want to make a breack-down per order to see which products are in the order and also can see the total price of the order and the price per product. 

In order.info.deposit you see the nett price per product, with that information  I want to measure the total nett price of the order. Now he is only capturing the first number/currency.

 

Robbie_Koetsier_0-1707740677339.png

Robbie_Koetsier_1-1707740682519.png

Hope someone can help me out.

1 Reply

Avatar

Level 4

Hi Robbie!

Assuming the data layer is implemented properly, and the indices of the products match with indices of the order, you can try the below code which will pass all the values into products variable in Adobe Analytics. This is valid if you are using App Measurement, or Adobe Analytics Extension in your TMS. If you are using Web SDK to implement Adobe Analytics, the code will different.  

PS: This is only one of the ways it can be done, and there are many ways one can iterate through a data layer. I used forEach in one, and map/join in second. Maybe you can try using traditional for loop only. Also, you might need to add necessary checks like data layer is not equal to null or undefined, so please ensure that is also done. 

 

 

const productString = (data) => {
    let products = '';

data.product.productInfo.name.forEach((item,index)=>{

    if(products!=''){
            products = products +",;"+item+";"+data.product.productInfo.quantity[index]+";"+data.product.productInfo.price[index]+";;"
    
    
} else {

        products = ";"+item+";"+data.product.productInfo.quantity[index]+";"+data.product.productInfo.price[index]+";;";
        
}
});

return products;
}

s.products = productString(dataLayer);

//Optimized Code using map and join and $ if you are comfortable using some latest JS

const productString = (data) => {
    const productsArray = data.product.productInfo.name.map((item, index) => {
        return `;${item};${data.product.productInfo.quantity[index]};${data.product.productInfo.price[index]};`;
    });

    return productsArray.join(",;");
};

 

 

The function iterates through all products and returns an appropriate product string for Adobe Analytics. Read more about products string if needed. 
https://experienceleague.adobe.com/docs/analytics/implementation/vars/page-vars/products.html?lang=e...

The output is something like this below.

V_Sirish_Kaushik1_0-1707766649900.png