toString — GTM Variable Template for String
toString CORE String
Converts a value to a string representation, or returns an empty string if the value is null or undefined.
When to Use This
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Type Conversion
Safely convert between data types — strings, numbers, booleans, arrays, objects.
Examples
Number to string
INPUT
Value To Convert: 42
OUTPUT
42
Boolean to string
INPUT
Value To Convert: true
OUTPUT
true
Null returns empty string
INPUT
Value To Convert: null
OUTPUT
""
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
toString
Value To Convert
💾 The value to convert to a string representation.
Supported formats:
✓ Any
Supported formats:
✓ Any
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.
Value To Convert any
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
toString()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Converts a value to a string.
*
* @param {any} data.src - The value to convert 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 conversion.
*
* @returns {string} The string representation of the value, or an empty string if the value is null or undefined.
*
* @framework ggLowCodeGTMKit
*/
const toString = function(value) {
if (value == null) { return ''; }
return value.toString();
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toString - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(toString(value));
// ===============================================================================
// toString() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(toString(value));
};
*/🧪 View Test Scenarios (7 tests)
✅ '[example] Number to string'
✅ '[example] Boolean to string'
✅ Array to string - converts array to comma-separated string
✅ '[example] Null returns empty string'
✅ Undefined value - returns empty string
✅ Object to string - converts object to string representation
✅ Function to string - converts function to empty string (no toString in sandbox)