Skip to content

assignObjectsFromPairs — GTM Variable Template for Object

VARIABLES › OBJECT
assignObjectsFromPairs EXTENDED Object

Merges key-value pair objects from a GTM table.



Examples

Merge pair objects
INPUT
Base Object: {name: 'John', age: 30}
ext: [{key: 'city', val: 'Paris'}, {key: 'country', val: 'France'}]
OUTPUT
John
Override existing keys
INPUT
Base Object: {name: 'John', age: 30, city: 'London'}
ext: [{key: 'age', val: 35}, {key: 'city', val: 'Paris'}]
OUTPUT
John

GTM Configuration

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

Read-only Preview
assignObjectsFromPairs
Base Object
💾 The base object to merge into.

Supported formats:
  ✓ Object
Properties to assign
💾 An object whose properties will override or extend the base object.

Supported formats:
  ✓ Object

*** Merge pair objects***

*** Override existing keys***
KeyValue
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.


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 makeTableMap = require('makeTableMap');

const assign = function(sourceObject, additionalObject) {
    // Copy all properties from additionalObject to sourceObject
    for (let key in additionalObject) {
        sourceObject[key] = additionalObject[key];
    }
    return sourceObject;
};

const toObjectPairs = function(simpleTable) {
    return makeTableMap(simpleTable, "key", "val");
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// assignObjectsFromPairs - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(assign(value, toObjectPairs(data.ext)));
// ===============================================================================
// assignObjectsFromPairs(...) – Apply Mode
// ===============================================================================
/*
return function(value, additionalObject) {
   additionalObject = data.rp1 ? data.ext : additionalObject;
   return out(assign(value, additionalObject));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Merge pair objects'
✅ '[example] Override existing keys'
✅ Test with empty additional object
✅ Test with empty source object
✅ Test with mixed value types