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
Section titled βWhen to Use ThisβGA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Examples
Section titled β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
Live Sandbox
Section titled βLive Sandboxβ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
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled β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 ? prop1 : data.pr1; prop2 = data.rp2 ? prop2 : data.pr2; 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'