Skip to content

trim β€” GTM Variable Template for String

VARIABLES β€Ί STRING
trim CORE String
Direct (.tpl) Apply (.tpl)

Trims any leading or trailing whitespace from a valid string. Returns an empty string when the input is not a valid string.


String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.


Trim leading and trailing spaces
INPUT
String To Trim: hello world
OUTPUT
hello world
Only spaces returns empty string
INPUT
String To Trim:
OUTPUT
""
Non-string returns empty string
INPUT
String To Trim: 123
OUTPUT
""

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

trim
String To Trim
πŸ’Ύ The value to be converted to a string and trimmed.

Supported formats:
  βœ“ String
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., normalize case, convert to string). Internal transformations such as string validation 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 + ' trimmed' for string modifications). Useful for chaining transformations on the output.
String To Trim string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
trim()


πŸ“œ View Implementation Code
/**
* Trims whitespace from both ends of a string.
*
* @param {any} data.src - The value to be converted to a string.
* @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 trimming.
*
* @returns {string} The string representation of the input with whitespace trimmed, or an empty string.
*
* @framework ggLowCodeGTMKit
*/
const trim = function(string) {
return typeof string === 'string' ? string.trim() : "";
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// trim - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(trim(value));
// ===============================================================================
// trim() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(trim(value));
};
*/
πŸ§ͺ View Test Scenarios (9 tests)
βœ… Trim both ends
βœ… String with only leading spaces - should trim start
βœ… String with only trailing spaces - should trim end
βœ… String with no whitespace - should return same string
βœ… '[example] Trim leading and trailing spaces'
βœ… '[example] Only spaces returns empty string'
βœ… Trim tabs and newlines
βœ… '[example] Non-string returns empty string'
βœ… Non-string input (null) - should return empty string