sumPropertyByPath — GTM Variable Template for Items
sumPropertyByPath CORE Items
Sums a specified numeric property across all objects. Supports nested paths. String numbers are auto-converted.
When to Use This
GA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Type Conversion
Safely convert between data types — strings, numbers, booleans, arrays, objects.
Examples
Sum simple property
INPUT
Array of Objects: [
{price: 10},
{price: 20},
{price: 30}
]
Property Path: price
{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
{product: {price: {amount: 100}}},
{product: {price: {amount: 200}}},
{product: {price: {amount: 150}}}
]
Property Path: product.price.amount
OUTPUT
450
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
sumPropertyByPath
Array of Objects
💾 The array of objects to sum values from.
Supported formats:
✓ Array variable: {{myArrayVariable}}
✓ Array literal
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"
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
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
sumBy()
Related Variables
Same category: Items
Under the Hood
📜 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 ? data.pth : path;
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