assignDefaults β GTM Variable Template for Object
assignDefaults EXTENDED Object
Fills in missing properties from a defaults object. Existing values are preserved.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβFill missing properties
INPUT
Value to Process: {name: "John", age: 30}
def: {age: 25, city: "Paris", country: "France"}
def: {age: 25, city: "Paris", country: "France"}
OUTPUT
{name: "John", age: 30, city: "Paris", country: "France"}
Existing values preserved
INPUT
Value to Process: {name: "Alice", age: 40, city: "London"}
def: {name: "Bob", age: 25, city: "Paris"}
def: {name: "Bob", age: 25, city: "Paris"}
OUTPUT
{name: "Alice", age: 40, city: "London"}
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.
assignDefaults
Source Object
πΎ The base object with initial values.
Default Values
πΎ An object containing default values to assign where undefined.
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Source Object string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Default Values string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
assignDefaults()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Assigns default values from the second object to the first. * * @param {Object} data.src - The base object with initial values. * @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 merging. * * @returns {Object} A new object with default values assigned where needed. * * @framework ggLowCodeGTMKit */const getType = require('getType');
const assignDefaults = function(sourceObject, defaultsObject) { const result = {}; if (getType(sourceObject) === 'object') { for (let key in sourceObject) { result[key] = sourceObject[key]; } } if (getType(defaultsObject) === 'object') { for (let key in defaultsObject) { if (result[key] === undefined) { result[key] = defaultsObject[key]; } } } return result;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// assignDefaults - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(assignDefaults(value, data.def));// ===============================================================================// assignDefaults(...) β Apply Mode// ===============================================================================/*return function(value, defaultsObject) { defaultsObject = data.rp1 ? defaultsObject : data.def; return out(assignDefaults(value, defaultsObject));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Fill missing properties'β
Empty base object - returns all defaultsβ
'[example] Existing values preserved'β
Non-object source - returns only defaultsβ
Both non-objects - returns empty object