sumPropertyNested β GTM Variable Template for Items
sumPropertyNested EXTENDED Items
Sums a specified numeric property across all objects. Supports nested paths. String numbers are auto-converted.
When to Use This
Section titled β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
Section titled β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
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.
sumPropertyNested
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
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled β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 ? 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