Skip to content

collapseWhitespace β€” GTM Variable Template for String

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

Sanitizes a string by replacing line breaks and tabs with spaces, collapsing multiple whitespace characters into a single space, and trimming leading/trailing whitespace.


Collapse multiple spaces
INPUT
Text To Sanitize: Hello world test
OUTPUT
Hello world test
Replace line breaks and tabs
INPUT
Text To Sanitize: Line1\nLine2\tTabbed\rReturn
OUTPUT
Line1 Line2 Tabbed Return
Non-string input returns undefined
INPUT
Text To Sanitize: 12345
OUTPUT
undefined

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

collapseWhitespace
Text To Sanitize
πŸ’Ύ The string to sanitize and clean.

Supported formats:
  βœ“ String
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., normalize encoding, convert to string). Internal transformations such as whitespace normalization will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), val => val + ' cleaned' for string modifications). Useful for chaining transformations on the output.
Text To Sanitize string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
collapseWhitespace()


πŸ“œ View Implementation Code
/**
* Sanitizes a string by:
* - Replacing line breaks and tabs with spaces
* - Collapsing multiple whitespace characters into a single space
* - Trimming leading and trailing whitespace
*
* @param {string} data.src - The string to sanitize.
* @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 sanitizing.
*
* @returns {string|undefined} The sanitized string or undefined if input is not a string.
*
* @framework ggLowCodeGTMKit
*/
const collapseWhitespace = function(input) {
if (typeof input !== 'string') {
return undefined;
}
let cleaned = input.split('\n').join(' ')
.split('\r').join(' ')
.split('\t').join(' ');
while (cleaned.indexOf(' ') !== -1) {
cleaned = cleaned.split(' ').join(' ');
}
return cleaned.trim();
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// collapseWhitespace - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(collapseWhitespace(value));
// ===============================================================================
// collapseWhitespace() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(collapseWhitespace(value));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Collapse multiple spaces'
βœ… '[example] Replace line breaks and tabs'
βœ… String with leading and trailing whitespace - trims edges
βœ… String with mixed whitespace issues - sanitizes completely
βœ… '[example] Non-string input returns undefined'