Skip to content

assignObjects — GTM Variable Template for Object

VARIABLES › OBJECT
assignObjects EXTENDED Object

Merges two objects. Later properties override earlier ones.



Examples

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"}

GTM Configuration

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


Under the Hood

📜 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 ? data.ext : additionalObject;
   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