concat β GTM Variable Template for String
concat EXTENDED String
Concatenates multiple values or arrays into a single array.
Examples
Section titled βExamplesβConcatenate with separator
INPUT
Values to Concatenate: [{val: 'apple'}, {val: 'banana'}, {val: 'orange'}]
Separator (use {{space}} for space, blank for no separator): ,
Separator (use {{space}} for space, blank for no separator): ,
OUTPUT
apple,banana,orange
Mixed types concatenation
INPUT
Values to Concatenate: [{val: 1}, {val: 2.5}, {val: 'three'}, {val: true}, {val: 0}]
Separator (use {{space}} for space, blank for no separator): |
Separator (use {{space}} for space, blank for no separator): |
OUTPUT
1|2.5|three|true|0
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.
concat
Values to Concatenate
πΎ List of values to concatenate. Each row represents one value.
Supported formats:
β String
β Number
β Boolean
β Variable reference
*** Concatenate with separator***
*** Mixed types concatenation***
Supported formats:
β String
β Number
β Boolean
β Variable reference
*** Concatenate with separator***
*** Mixed types concatenation***
β
Separator (use {{space}} for space, blank for no separator)
πΎ The separator string to place between concatenated values.
Special encoding: Use
Supported formats:
β String (use {{space}} for space)
β Empty string (no separator)
Special encoding: Use
{{space}} to represent a space character (spaces are trimmed in GTM fields).Supported formats:
β String (use {{space}} for space)
β Empty string (no separator)
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the extracted values array before concatenation (e.g., filter empty values, transform array). Note: Values are extracted from the table's
val property before this function is applied.Result Handling
Output Function (optional)
βοΈ Optional function to apply to the concatenated result before returning it (e.g.,
str => str.toUpperCase(), str => str.trim()). Useful for chaining transformations on the output.Values to Concatenate list
Separator (use {{space}} for space, blank for no separator) string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
concat()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Concatenates the values in an array, with an optional separator. * * @param {Array} data.src - An array of objects (Direct mode) or values (Apply mode) to concatenate. * @param {string} data.sep - The separator string to use (use {{space}} for spaces). * @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 processing. * * @returns {string} The concatenated string of values, separated by the provided separator. * * @framework ggLowCodeGTMKit */const toString = (value) => (value == null ? '' : value.toString());
const concat = function(valueArray, separator) { const values = valueArray || []; return values.map(val => toString(val)).join(separator);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// concat - Direct mode// ===============================================================================const extractItemValues = (items) => items.map(item => item ? item.val : null);const applyCast = (castFn, value) => safeFunction(castFn)(value);const extractedValues = extractItemValues(data.src);const value = applyCast(data.pre, extractedValues);return out(concat(value, data.sep));// ===============================================================================// concat(...) β Apply Mode// ===============================================================================/*return function(value, separator) { return out(concat(value, data.sep));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Concatenate with separator'β
Test concatenation with space separatorβ
Test with empty arrayβ
Test with null and undefined values in arrayβ
'[example] Mixed types concatenation'