Skip to content

sumPropertyNested β€” GTM Variable Template for Items

VARIABLES β€Ί ITEMS
sumPropertyNested EXTENDED Items
Direct (.tpl) Apply (.tpl)

Sums a specified numeric property across all objects. Supports nested paths. String numbers are auto-converted.


GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.

Type Conversion

Safely convert between data types β€” strings, numbers, booleans, arrays, objects.


Sum simple property
INPUT
Array of Objects: [
{price: 10},
{price: 20},
{price: 30}
]
Property Path:
price
OUTPUT
60
Sum nested property
INPUT
Array of Objects: [
{product: {price: {amount: 100}}},
{product: {price: {amount: 200}}},
{product: {price: {amount: 150}}}
]
Property Path:
product.price.amount
OUTPUT
450

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

sumPropertyNested
Array of Objects
πŸ’Ύ The array of objects to sum values from.

Supported formats:
  βœ“ Array variable: {{myArrayVariable}}
  βœ“ Array literal
Property Path
πŸ’Ύ The path to the numeric property to sum. Uses dot notation for nested properties. Non-numeric values are ignored.

Supported formats:
  βœ“ String (dot notation): "price", "product.price", "order.total.amount"
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the array before summing (e.g., filter specific items, transform array).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the sum before returning it (e.g., sum => sum.toFixed(2), sum => sum / 100 for cents to dollars). Useful for chaining transformations on the output.
Array of Objects array
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Property Path string
sumBy()


πŸ“œ View Implementation Code
/**
* Sums a numeric property across all objects in an array, using a path string.
*
* @param {Array} data.src - The array of objects.
* @param {string} data.pth - The path to the numeric value (e.g., "price.amount").
* @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 processing.
*
* @returns {number} The sum of the values found at the specified path. Returns 0 if array is invalid or empty.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');
const getNested = function(obj, path) {
const parts = path.split('.');
let current = obj;
for (let i = 0; i < parts.length; i++) {
if (current == null || !current.hasOwnProperty(parts[i])) {
return undefined;
}
current = current[parts[i]];
}
return current;
};
const sumBy = function(array, path) {
if (getType(array) !== 'array' || typeof path !== 'string') {
return 0;
}
let sum = 0;
for (let i = 0; i < array.length; i++) {
const value = makeNumber(getNested(array[i], path));
if (typeof value === 'number' && value === value) {
sum += value;
}
}
return sum;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// sumPropertyByPath - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(sumBy(value, data.pth));
// ===============================================================================
// sumPropertyByPath(...) – Apply Mode
// ===============================================================================
/*
return function(array, path) {
path = data.rp1 ? path : data.pth;
return out(sumBy(array, path));
};
*/
πŸ§ͺ View Test Scenarios (7 tests)
βœ… '[example] Sum simple property'
βœ… '[example] Sum nested property'
βœ… Test with missing property in some objects
βœ… Test with string numbers converted to numbers
βœ… Test with empty array returns 0
βœ… Test with non-numeric values skipped
βœ… Test with negative numbers