mergeConcat β GTM Variable Template for Object
mergeConcat EXTENDED Object
Merges two objects, concatenating arrays instead of replacing them.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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
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.
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
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled β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 ? 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