itemAssignDefaults — GTM Variable Template for Items
itemAssignDefaults EXTENDED Items
Assigns default values to missing or undefined properties on each object in an array. Existing values are preserved.
Examples
Assign missing defaults
INPUT
Source Array: [
{name: 'Shirt', price: 10},
{name: 'Pants'}
]
Default Values: [
{key: 'price', val: 0},
{key: 'quantity', val: 1},
{key: 'currency', val: 'EUR'}
]
{name: 'Shirt', price: 10},
{name: 'Pants'}
]
Default Values: [
{key: 'price', val: 0},
{key: 'quantity', val: 1},
{key: 'currency', val: 'EUR'}
]
OUTPUT
[ {name: 'Shirt', price: 10, quantity: 1, currency: 'EUR'}, {name: 'Pants', price: 0, quantity: 1, currency: 'EUR'} ]
Existing values preserved
INPUT
Source Array: [
{name: 'Shirt', price: 10, quantity: 3}
]
Default Values: [
{key: 'quantity', val: 1},
{key: 'currency', val: 'EUR'}
]
{name: 'Shirt', price: 10, quantity: 3}
]
Default Values: [
{key: 'quantity', val: 1},
{key: 'currency', val: 'EUR'}
]
OUTPUT
[ {name: 'Shirt', price: 10, quantity: 3, currency: 'EUR'} ]
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
itemAssignDefaults
Source Array
💾 The array of objects to process.
Supported formats:
✓ Items variable: {{ecommerce.items}}
✓ Array literal: [{item_id: "SKU001", price: 29.99}]
Supported formats:
✓ Items variable: {{ecommerce.items}}
✓ Array literal: [{item_id: "SKU001", price: 29.99}]
Default Values
💾 The property name to assign a default value to (e.g., "price", "quantity", "currency"). 💾 The default value to assign if the property is missing or undefined (e.g., 0, 1, "EUR", "Unknown").
*** Assign missing defaults***
*** Existing values preserved***
*** Assign missing defaults***
*** Existing values preserved***
Property NameDefault Value
⊖
⊖
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the source array before processing.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the resulting array before returning it (e.g.,
arr => arr.filter(x => x.price > 0) to keep only items with a positive price).Source Array array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Default Values table
Property NameDefault Value
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemAssignDefaults()
Related Variables
Same category: Items
Under the Hood
📜 View Implementation Code
/**
* Assigns default values to missing or undefined properties on each object in an array.
* Only fills properties that are undefined on each item — existing values are preserved.
*
* @param {Array} data.src - The array of objects to process.
* @param {Array} data.dfs - Array of objects with property name (key) and default value (val) pairs.
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before processing.
*
* @returns {Array} A new array of objects with default values assigned to missing properties.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const itemAssignDefaults = function(arr, defaults) {
if (getType(arr) !== 'array') return [];
if (!defaults || defaults.length === 0) return arr;
return arr.map(function(item) {
if (item == null || typeof item !== 'object') return item;
const result = {};
for (const k in item) {
if (item.hasOwnProperty(k)) result[k] = item[k];
}
for (let i = 0; i < defaults.length; i++) {
const key = defaults[i].key;
const val = defaults[i].val;
if (key && result[key] === undefined) {
result[key] = val;
}
}
return result;
});
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// itemAssignDefaults - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(itemAssignDefaults(value, data.dfs));
// ===============================================================================
// itemAssignDefaults(...) – Apply Mode
// ===============================================================================
/*
return function(arr) {
return out(itemAssignDefaults(arr, data.dfs));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Assign missing defaults'
✅ '[example] Existing values preserved'
✅ Does not overwrite falsy values (0, false, empty string)
✅ Empty defaults returns original array
✅ Invalid src returns empty array