Skip to content

assignObjects β€” GTM Variable Template for Object

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

Merges two objects. Later properties override earlier ones.



Merge two objects
INPUT
Base Object: {name: "John", age: 30}
Extension Object: {city: "Paris", country: "France"}
OUTPUT
{name: "John", age: 30, city: "Paris", country: "France"}
Override on conflict
INPUT
Base Object: {name: "John", age: 30, city: "London"}
Extension Object: {city: "Paris", country: "France"}
OUTPUT
{name: "John", age: 30, city: "Paris", country: "France"}

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

assignObjects
Base Object
πŸ’Ύ The base object to merge into.
Extension Object
πŸ’Ύ An object whose properties will override or extend the base object.
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.
Base Object object
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Extension Object object
assign()


πŸ“œ View Implementation Code
/**
* Assigns properties from two objects into a new object.
*
* @param {Object} data.src - The base object.
* @param {Object} data.ext - An object whose properties will override or extend the base object.
* @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 containing properties from both inputs.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const assign = function(sourceObject, additionalObject) {
const result = {};
if (getType(sourceObject) === 'object') {
for (let key in sourceObject) {
result[key] = sourceObject[key];
}
}
if (getType(additionalObject) === 'object') {
for (let key in additionalObject) {
result[key] = additionalObject[key];
}
}
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// assignObjects - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(assign(value, data.ext));
// ===============================================================================
// assignObjects(...) – Apply Mode
// ===============================================================================
/*
return function(value, additionalObject) {
additionalObject = data.rp1 ? additionalObject : data.ext;
return out(assign(value, additionalObject));
};
*/
πŸ§ͺ View Test Scenarios (8 tests)
βœ… '[example] Merge two objects'
βœ… '[example] Override on conflict'
βœ… Empty source object - returns only extension properties
βœ… Empty extension object - returns only source properties
βœ… Both empty objects - returns empty object
βœ… Non-object source - handles invalid source
βœ… Non-object extension - handles invalid extension
βœ… Both non-objects - handles both invalid