Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Not applicable
I have a variable that stores the line cost of products purchased and i also have a quantity field. I want to populate item price based on (line cost / quantity). Is this possible or do i need to get the variable declared on my page?
7 REPLIES 7
Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Employee Emeritus
Hi Andrew, You can run an extension before the E-Commerce extension which will do this calculation for you and put it in a new variable - then you can map that in the E-Commerce extension to the 'prices' field. Here is how: - Create a data source called 'product_prices' - Set 'product_prices' as the 'prices' element of the E-Commerce extension - Create a 'javascript' extension and insert the following code: {code:language} //Copy original variables to new vars for simplicity line_price = b['product_line_price'] || undefined; quantities = b['product_quantities'] || undefined; // Note: Change 'product_line_price' for the name of your line prices data source above // Note: Change 'product_quantities' for the name of your quantities data source above //Loop through 'line_price' and 'quantities' to calculate prices if(line_price && quantities){ //Create new variable which we will map in E-Commerce extension for 'prices' b.product_prices = []; for (var i = 0; i < line_price.length; i++) { single_line_price = parseFloat(line_price[i]); //Take line price and convert to number single_quantity = parseFloat(quantities[i]); //Take quantities and convert to number single_price = single_line_price/single_quantity; //Calculate price of item single_price = single_price.toString(); //Convert price into string b.product_prices.push(single_price); //Push price into our new array containing the prices } } {code} Note: Be sure to change the 'product_line_price' and 'product_quantities' to the names of the variables you're going to pull from. This should now work! Let me know if you have any issues.
Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Not applicable
Hi Roshan Thanks for such a quick response! I've tried this but it's not worked. A few questions; - When creating the new data source ' product_prices' - should this be a javascript page variable? - When validating the new javascript extension i get the following error message; "TypeError: Cannot read property 'booking_amount' of undefined - Ignore Error" If i remove the line for 'booking_amount', I also get an error for 'quantity' 'booking_amount' & 'quantity' are both "data layer" data sources. Thanks Andrew
Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Not applicable
In addition does it matter which order my extensions appear? Currently my new javascript extension appears below my ecommerce extension, should it be the other way round?
Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Employee Emeritus
Hey Andrew, Yes, you'll need to put the 'javascript' extension above the E-Commerce extension for it to work. For the new 'product_prices' data source, set this as a 'data layer' data source. If that doesn't work, I think you might need a bit more help on this though, could you let me know your account name and is there a page I can test on? Let me know if my suggestion doesn't fix.
Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Not applicable
Hi Roshan I've made the suggested amends. This is what i'm getting via Tealium tag manager { "booking_amount" : "6476", "booking_reference" : "5125", "currency" : "GBP", "dept_code" : "CBSE140824-XL", "event_type" : "booking", "product_prices" : [ "3", "NaN", "NaN", "NaN" ], "quantity" : "2", "sku_list" : "CBSE", "sku_name" : "China Experience" } As you can see it's setting product_prices to be "3", "NaN", "NaN", "NaN" I would expect it to be 3238 (booking_amount 6476 / quantity 2). Note that we sell holidays and as such there is only ever 1 sku per order. Account is tui Profile tac These orders are being placed via a test site that is restricted by password. I can email these to you separately if you need. Thanks Andrew
Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Employee Emeritus
Ah - thanks Andrew. I didn't realise you weren't using an array for your products. This makes life a lot easier for this case :) Here is an update piece of code for you - you can just overwrite the old code: {code:language} //Check whether 'booking_amount' and 'quantity' are set in the data layer if(b['booking_amount'] && b['quantity']){ //Minify amount = b['booking_amount']; quant = b['quantity']; //Convert to numbers amount = parseFloat(amount); quant = parseFloat(quant); //Calculate price per item product_prices = amount/quant; //Convert into string and push into data layer b['product_prices'] = product_prices.toString(); } {code} Let me know if that works!
Highlighted

Is it possible to have a variable in Ecommerce extension based on a calculation of two other data variables

Not applicable
Works perfectly, thanks!