Skip to content

sumProperty — GTM Variable Template for Items

VARIABLES › ITEMS
sumProperty CORE Items

Sums a specified property across all objects in an array. Non-numeric values are ignored.


When to Use This

GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.


Examples

Sum numeric property
INPUT
Input Array: [{price: 10}, {price: 20}, {price: 30}]
Property Name: price
OUTPUT
60
Ignores invalid values
INPUT
Input Array: [{count: 5}, {count: 'invalid'}, {count: 10}, {count: null}]
Property Name: count
OUTPUT
15

GTM Configuration

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
Property Name
💾 The name of the numeric property to sum.

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
sumProperty()


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 ? data.prp : property;
   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