Skip to content

assignDefaultsDeep — GTM Variable Template for Object

VARIABLES › OBJECT
assignDefaultsDeep EXTENDED Object

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



Examples

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

GTM Configuration

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()


Under the Hood

📜 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 ? data.def : defaultsObject;
   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