Sum of items in ecommerce extension

Gold Contributor
Gold Contributor
Looking to see if there's a UDO object already available in the ecommerce extension for a sum of the number of items ordered, or if not, how to get this via JS.
3 REPLIES 3

Sum of items in ecommerce extension

Employee Emeritus
Patrick At Tealium we do not like to utilize the javaScript Extension unless it is necessary. In your case we do not have an extension that will sum values in an array, so we need javaScript to help us out. Here is what the code below is doing: When any event occurs Check to make sure the user is on the receipt page Then check to make sure product_quantity is an array If those checks are true then create a order_item_count and set it to zero. Then loop through all the product quantities and add them to the count Finally return the value in string format to follow our best practices. if(b['page_type'] === 'receipt' && utag.ut.typeOf(b['product_quantity']) === 'array'){ b['order_item_count'] = 0; for(var i in b['product_quantity']){ b['order_item_count'] += parseInt(b['product_quantity'][i]); } b['order_item_count'] = b['order_item_count'] + ""; } I will also add another comment with a screenshot of the extension. The code assumes your order confirmation page has the page_type of receipt, your product array has the name product_quantity and the final data source you are wanting to populate is order_item_count. Also this code assumes you are using utag version 4.35 or greater so that utag.ut.typeof will be available. If any of this is confusing, please feel free to reach out to your Account Manager to get an engineer to look at your specific use case. Happy Tracking! Bk

Sum of items in ecommerce extension

Gold Contributor
Gold Contributor
Thanks. Below is the "commerce array modification" JS we have - how would this fit in? if (utag_data.item_ids) { var item_ids_array = b.item_ids; var item_qty_array = b.item_qty; var item_description_array = b.item_description; var item_discount_array = b.item_discount; var item_revenue_array = b.item_revenue; var item_events_array = new Array(); var total_revenue = 0; for (var i = 0; i < item_ids_array.length; i++) { //item_ids_array[i] = "awco-" + item_ids_array[i]; item_description_array[i] = item_ids_array[i] + "|" + item_description_array[i]; total_revenue = total_revenue + parseFloat(item_revenue_array[i]); item_events_array[i] = ""; } if (b.order_id) { // proc fee item_ids_array[i] = "ProcFee"; item_qty_array[i] = "1"; item_revenue_array[i] = String(total_revenue); item_events_array[i] = {'event41': String(total_revenue)}; item_description_array[i] = "ProcFee"; // amount paid item_ids_array[i+1] = "AmountPaid"; item_qty_array[i+1] = ""; item_revenue_array[i+1] = ""; item_events_array[i+1]= {'event42': b.amount_paid}; item_description_array[+i] = "AmountPaid"; // set sc_product_events b.sc_prodevents = item_events_array; } }

Sum of items in ecommerce extension

Employee Emeritus
Patrick At this point, I suggest you should reach out to your Account Manager to have an engineer look at your specific use case and website to be able to test any javascript code before releasing. However my guess is they will have you add two lines of code var total_item_count = 0; //insert below var total_revenue = 0; total_item_count += parseInt(item_qty_array[i]); // insert below total_revenue = total_revenue + parseFloat(item_revenue_array[i]); This code has not been tested!
Public