toStringRepresentation β GTM Variable Template for String
toStringRepresentation EXTENDED String
Converts the provided value to its string representation. Returns the string null for null and undefined for undefined.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβNumber to string
INPUT
Value To Convert: 42
OUTPUT
42
Null returns the string null
INPUT
Value To Convert: null
OUTPUT
null
Undefined returns the string undefined
INPUT
Value To Convert: undefined
OUTPUT
undefined
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
toStringRepresentation
Value To Convert
πΎ The value to convert to a string.
Supported formats:
β Any
Supported formats:
β Any
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the value before internal logic (e.g., normalize data, apply calculations). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), str => str.trim() for whitespace removal). 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.
toStringRepresentation()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Converts a value to a string. * * @param {any} data.val - 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 val before conversion. * * @returns {string} The string representation of the value, or "null" or "undefined" if the value is null or undefined. * * @framework ggLowCodeGTMKit */
const toStringRepresentation = function(value) { if (value === null) { return 'null'; } if (value === undefined) { return 'undefined'; } return value.toString();};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// toStringRepresentation - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedValue = applyCast(data.pre, data.val);return out(toStringRepresentation(processedValue));// ===============================================================================// toStringRepresentation() β Apply Mode// ===============================================================================/*return function(value) { return out(toStringRepresentation(value));};*/π§ͺ View Test Scenarios (10 tests)
β
'[example] Number to string'β
Boolean true to string - returns true stringβ
Boolean false to string - returns false stringβ
Array to string - converts array to comma-separated stringβ
Object to string - converts object to string representationβ
'[example] Null returns the string null'β
'[example] Undefined returns the string undefined'β
Empty string - returns empty stringβ
Zero number - converts to 0 stringβ
Negative number - converts to string with minus sign