sumProductOfProperties — GTM Variable Template for Items
sumProductOfProperties CORE Items
Multiplies two numeric properties for each object and sums the results. Useful for price × quantity totals.
When to Use This
GA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Examples
Sum of price × quantity
INPUT
Input Array: [
{price: 10, quantity: 2},
{price: 20, quantity: 3},
{price: 30, quantity: 1}
]
{price: 10, quantity: 2},
{price: 20, quantity: 3},
{price: 30, quantity: 1}
]
OUTPUT
110
Empty array returns 0
INPUT
Input Array: []
First Property Name: price
Second Property Name: quantity
First Property Name: price
Second Property Name: quantity
OUTPUT
0
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
sumProductOfProperties
Input Array
💾 The array of objects.
Supported formats:
✓ Array
Supported formats:
✓ Array
First Property Name
💾 The name of the first numeric property to multiply (e.g., "price").
Supported formats:
✓ String
Supported formats:
✓ String
Second Property Name
💾 The name of the second numeric property to multiply (e.g., "quantity").
Supported formats:
✓ String
Supported formats:
✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Input Array array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
First Property Name string
Second Property Name string
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
sumProductOfProperties()
Related Variables
Same category: Items
Under the Hood
📜 View Implementation Code
/**
* Sums the product of two numeric properties across all objects in an array.
*
* @param {Array} data.src - The array of objects.
* @param {string} data.pr1 - The first numeric property to multiply (e.g., "price").
* @param {string} data.pr2 - The second numeric property to multiply (e.g., "quantity").
* @param {Function|string} [data.out] - Optional output handler.
*
* @returns {number} The sum of (value1 * value2) for each item. Returns 0 if array is invalid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');
const sumProductOfProperties = function(arr, prop1, prop2) {
if (getType(arr) !== 'array' || typeof prop1 !== 'string' || typeof prop2 !== 'string') {
return 0;
}
let sum = 0;
for (let i = 0; i < arr.length; i++) {
const v1 = makeNumber(arr[i][prop1]);
const v2 = makeNumber(arr[i][prop2]);
if (typeof v1 === 'number' && v1 === v1 && typeof v2 === 'number' && v2 === v2) {
sum += v1 * v2;
}
}
return sum;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// sumProductOfProperties - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(sumProductOfProperties(value, data.pr1, data.pr2));
// ===============================================================================
// sumProductOfProperties - Apply mode
// ===============================================================================
/*
return function(arr, prop1, prop2) {
prop1 = data.rp1 ? data.pr1 : prop1;
prop2 = data.rp2 ? data.pr2 : prop2;
return out(sumProductOfProperties(arr, prop1, prop2));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Sum of price × quantity'
✅ Skips invalid (non-numeric) values
✅ Returns 0 for invalid array input
✅ Returns 0 for invalid property names
✅ '[example] Empty array returns 0'