Skip to content

concat β€” GTM Variable Template for String

VARIABLES β€Ί STRING
concat EXTENDED String
Direct (.tpl) Apply (.tpl)

Concatenates multiple values or arrays into a single array.


Concatenate with separator
INPUT
Values to Concatenate: [{val: 'apple'}, {val: 'banana'}, {val: 'orange'}]
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): |
OUTPUT
1|2.5|three|true|0

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***
βŠ–
Separator (use {{space}} for space, blank for no separator)
πŸ’Ύ The separator string to place between concatenated values.

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
concat()


πŸ“œ 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'