assignDefaults — GTM Variable Template for Object
assignDefaults EXTENDED Object
Fills in missing properties from a defaults object. Existing values are preserved.
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"}
GTM Configuration
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
Same category: Object
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 ? data.def : defaultsObject;
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