Skip to content

mergeConcat β€” GTM Variable Template for Object

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

Merges two objects, concatenating arrays instead of replacing them.



Arrays concatenated on merge
INPUT
Base Object: {name: 'John', tags: ['user', 'active']}
Additional Object: {age: 30, tags: ['premium']}
OUTPUT
John
Non-array properties override
INPUT
Base Object: {status: 'pending', priority: 1, count: 5}
Additional Object: {status: 'completed', count: 10}
OUTPUT
completed

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

mergeConcat
Base Object
πŸ’Ύ The base object to merge from.

Supported formats:
  βœ“ Object variable: {{myObject}}
  βœ“ Object literal
Additional Object
πŸ’Ύ The additional object to merge in. Properties override base object, except arrays which are concatenated.

Supported formats:
  βœ“ Object variable: {{anotherObject}}
  βœ“ Object literal
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the base object before merging (e.g., normalize structure, parse JSON).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the merged object before returning it (e.g., obj => JSON.stringify(obj), obj => Object.freeze(obj)). 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
Additional Object object
mergeConcat()


πŸ“œ View Implementation Code
/**
* Shallow merges two objects, concatenating arrays instead of replacing them.
*
* @param {Object} data.src - The base object to merge from.
* @param {Object} data.add - The additional object to merge in.
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function.
*
* @returns {Object} A new object with merged properties. Arrays are concatenated, nested objects are replaced.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const mergeConcat = function(baseObject, additionalObject) {
const mergedObject = {};
if (!baseObject || typeof baseObject !== 'object') {
return additionalObject || {};
}
if (!additionalObject || typeof additionalObject !== 'object') {
return baseObject;
}
for (let key in baseObject) {
if (baseObject.hasOwnProperty(key)) {
if (getType(baseObject[key]) === 'array' && getType(additionalObject[key]) === 'array') {
mergedObject[key] = baseObject[key].concat(additionalObject[key]);
} else if (additionalObject[key] !== undefined) {
mergedObject[key] = additionalObject[key];
} else {
mergedObject[key] = baseObject[key];
}
}
}
for (let key in additionalObject) {
if (additionalObject.hasOwnProperty(key) && baseObject[key] === undefined) {
mergedObject[key] = additionalObject[key];
}
}
return mergedObject;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// mergeConcat- Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(mergeConcat(value, data.add));
// ===============================================================================
// mergeConcat(...) – Apply Mode
// ===============================================================================
/*
return function(baseObject, additionalObject) {
additionalObject = data.rp1 ? additionalObject : data.add;
return out(mergeConcat(baseObject, additionalObject));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Arrays concatenated on merge'
βœ… '[example] Non-array properties override'
βœ… Test adding new properties
βœ… Test empty additional object
βœ… Test array with one empty array