assignObjects β GTM Variable Template for Object
assignObjects EXTENDED Object
Merges two objects. Later properties override earlier ones.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβMerge two objects
INPUT
Base Object: {name: "John", age: 30}
Extension Object: {city: "Paris", country: "France"}
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"}
Extension Object: {city: "Paris", country: "France"}
OUTPUT
{name: "John", age: 30, city: "Paris", country: "France"}
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.
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
assign()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled β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 ? 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