Skip to content

toStringRepresentation β€” GTM Variable Template for String

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

Converts the provided value to its string representation. Returns the string null for null and undefined for undefined.



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

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
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
toStringRepresentation()


πŸ“œ 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