Skip to content

itemAssignDefaults β€” GTM Variable Template for Items

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

Assigns default values to missing or undefined properties on each object in an array. Existing values are preserved.



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'}
]
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'}
]
OUTPUT
[ {name: 'Shirt', price: 10, quantity: 3, currency: 'EUR'} ]

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}]
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***
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
itemAssignDefaults()


πŸ“œ 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