mergeConcat — GTM Variable Template for Object
mergeConcat EXTENDED Object
Merges two objects, concatenating arrays instead of replacing them.
Examples
Arrays concatenated on merge
INPUT
Base Object: {name: 'John', tags: ['user', 'active']}
Additional Object: {age: 30, tags: ['premium']}
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}
Additional Object: {status: 'completed', count: 10}
OUTPUT
completed
GTM Configuration
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
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
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
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
mergeConcat()
Related Variables
Same category: Object
Under the Hood
📜 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 ? data.add : additionalObject;
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