Skip to content

assignDefaultsDeep β€” GTM Variable Template for Object

VARIABLES β€Ί OBJECT
assignDefaultsDeep EXTENDED Object
Direct (.tpl) Apply (.tpl)

Recursively fills in missing properties from a defaults object, deeply nested.



Deep fill missing
INPUT
Base Object: {name: "John", age: 30}
Defaults Object: {name: "Default", age: 0, email: "[email protected]", role: "user"}
OUTPUT
John
Existing values preserved
INPUT
Base Object: {status: "active", count: 5}
Defaults Object: {status: "inactive", count: 0, type: "default"}
OUTPUT
active

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

assignDefaultsDeep
Base Object
πŸ’Ύ The base object to which defaults will be applied. Existing properties will not be overwritten.

Supported formats:
  βœ“ Object variable: {{myObject}}
  βœ“ Object literal
Defaults Object
πŸ’Ύ An object containing default values. Only properties that are undefined in the base object will be filled in, including nested properties.

Supported formats:
  βœ“ Object variable: {{defaultsObject}}
  βœ“ Object literal
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the base object before applying defaults (e.g., normalize object structure, filter properties).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result object before returning it (e.g., obj => JSON.stringify(obj), obj => Object.freeze(obj)). Useful for chaining transformations on the output.
Base Object object
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Defaults Object object
defaultsDeep()


πŸ“œ View Implementation Code
/**
* Recursively assigns default values from one object to another.
* Only properties that are `undefined` will be filled in, including nested ones.
*
* @param {Object} data.src - The base object to assign defaults to.
* @param {Object} data.def - An object containing default values.
* @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 {Object} A new object with deeply applied default values.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const defaultsDeep = function(target, defaults) {
const result = {};
// Copy or recurse from target
for (let key in target) {
if (getType(target[key]) === 'object' && getType(defaults[key]) === 'object') {
result[key] = defaultsDeep(target[key], defaults[key]);
} else {
result[key] = target[key];
}
}
// Fill in missing keys from defaults
for (let key in defaults) {
if (typeof result[key] === 'undefined') {
result[key] = defaults[key];
}
}
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// defaultsDeep - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(defaultsDeep(value, data.def));
// ===============================================================================
// defaultsDeep(...) – Apply Mode
// ===============================================================================
/*
return function(targetObject, defaultsObject) {
defaultsObject = data.rp1 ? defaultsObject : data.def;
return out(defaultsDeep(targetObject, defaultsObject));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Deep fill missing'
βœ… Deeply nested defaults should be applied recursively
βœ… '[example] Existing values preserved'
βœ… Multiple levels of nesting with partial defaults
βœ… Empty source object should receive all defaults