assignDefaultsDeep β GTM Variable Template for Object
assignDefaultsDeep EXTENDED Object
Recursively fills in missing properties from a defaults object, deeply nested.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβDeep fill missing
INPUT
Base Object: {name: "John", age: 30}
Defaults Object: {name: "Default", age: 0, email: "[email protected]", role: "user"}
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"}
Defaults Object: {status: "inactive", count: 0, type: "default"}
OUTPUT
active
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.
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
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
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
defaultsDeep()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled β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 ? 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