sumProperty β GTM Variable Template for Items
sumProperty CORE Items
Sums a specified property across all objects in an array. Non-numeric values are ignored.
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 numeric property
INPUT
Input Array: [{price: 10}, {price: 20}, {price: 30}]
Property Name: price
Property Name: price
OUTPUT
60
Ignores invalid values
INPUT
Input Array: [{count: 5}, {count: 'invalid'}, {count: 10}, {count: null}]
Property Name: count
Property Name: count
OUTPUT
15
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.
sumProperty
Input Array
πΎ The array of objects.
Supported formats:
β Array
Supported formats:
β Array
Property Name
πΎ The name of the numeric property to sum.
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
Property Name string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
sumProperty()
Related Variables
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Sums the specified property across all objects in the array.** @param {Array} data.src - The array of objects.* @param {string} data.prp - The name of the numeric property to sum.* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src before summing.** @returns {number} The total sum of the specified property.** @framework ggLowCodeGTMKit*/const getType = require('getType');const makeNumber = require('makeNumber');const sumProperty = function(arr, property) { if (getType(arr) !== 'array' || typeof property !== 'string') { return 0; }
let sum = 0; for (let i = 0; i < arr.length; i++) { const value = makeNumber(arr[i][property]); if (typeof value === 'number' && value === value) { sum += value; } } return sum;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// sumProperty - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedArray = applyCast(data.pre, data.src);return out(sumProperty(processedArray, data.prp));// ===============================================================================// sumProperty(...) β Apply Mode// ===============================================================================/*return function(value, property) { property = data.rp1 ? property : data.prp; return out(sumProperty(value, property));};*/π§ͺ View Test Scenarios (12 tests)
β
'[example] Sum numeric property'β
Array of objects with string numbers - should convert and sumβ
'[example] Ignores invalid values'β
Array of objects with decimal values - should return correct sumβ
Array of objects with negative numbers - should sum correctlyβ
Array of objects with zero values - should return 0β
Empty array - should return 0β
Array of objects with missing property - should return 0β
Array of objects with some missing property - should sum only existingβ
Non-array input - should return 0β
Non-string property - should return 0β
Array of objects with undefined values - should ignore undefined