Skip to content

truncate — GTM Variable Template for String

VARIABLES › STRING
truncate CORE String

Truncates a string if it is longer than the specified maximum length. The last characters are replaced with the omission string (default ...).


When to Use This

String Manipulation

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


Examples

Truncate with ellipsis
INPUT
String To Truncate: hello world
Max Length: 8
Use Custom Omission: false
Custom Omission:
OUTPUT
hello...
Short string unchanged
INPUT
String To Truncate: hello
Max Length: 10
Use Custom Omission: false
Custom Omission:
OUTPUT
hello

GTM Configuration

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

truncate
String To Truncate
💾 The string to truncate.

Supported formats:
  ✓ String
Max Length
💾 The maximum length the string can be before truncation.

Supported formats:
  ✓ Number
💾 Flag to determine if custom omission should be used instead of default '...'.

Supported formats:
  ✓ Boolean
Custom Omission
💾 The custom omission string to append to the truncated string.

Supported formats:
  ✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
String To Truncate string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Max Length number
Custom Omission string
truncate()


Under the Hood

📜 View Implementation Code
/**
 * Truncates a string if it is longer than the specified maximum length.
 * The last characters of the truncated string are replaced with the omission string (default is "...").
 *
 * @param {string} data.src - The string to truncate.
 * @param {number} data.max - The maximum length the string can be before truncation.
 * @param {boolean} data.add - Flag to determine if custom omission should be used.
 * @param {string} data.chr - The custom omission string to append to the truncated 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 truncating.
 * 
 * @returns {string} The truncated string with the omission string appended, or the original string if it is shorter than the maximum length.
 *
 * @framework ggLowCodeGTMKit
 */
const truncate = function(string, maxLength, addCustomOmission, customChars) {
    const omission = addCustomOmission ? customChars : "...";
    if (typeof string !== 'string' || string.length <= maxLength) {
        return string; 
    }
    return string.slice(0, maxLength - omission.length) + omission;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// truncate - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(truncate(value, data.max, data.add, data.chr));
// ===============================================================================
// truncate(...) – Apply Mode
// ===============================================================================
/*
return function(value, maxLength, addCustomOmission, customChars) {
   maxLength = data.rp1 ? data.max : maxLength;
   return out(truncate(value, maxLength, data.add, data.chr));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Truncate with ellipsis'
✅ String longer than max length with custom omission - should truncate with custom chars
✅ '[example] Short string unchanged'
✅ String equal to max length - should return unchanged
✅ Non-string input - should return unchanged