Skip to content

nullTo — GTM Variable Template for Value

VARIABLES › VALUE
nullTo CORE Value

Replaces null values with a default. Non-null values pass through unchanged.


When to Use This

Value Utilities

Handle edge cases — defaults for undefined/null, type checks, coercion.


Examples

String passes through
INPUT
Value To Check: hello
Default Value: default
OUTPUT
hello
Null returns default
INPUT
Value To Check: null
Default Value: default
OUTPUT
default

GTM Configuration

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

nullTo
Value To Check
💾 The value to check for null.

Supported formats:
  ✓ Any
Default Value
💾 The default value to return if the source value is null.

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 Check any
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Default Value any
nullTo()


Under the Hood

📜 View Implementation Code
/**
 * Replaces null with a specific default value.
 * 
 * @param {any} data.src - The value to check.
 * @param {any} data.def - The default value to return if the value is null.
 * @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 checking.
 * 
 * @returns {any} Returns the original value if it's not null, otherwise returns the default value.
 *
 * @framework ggLowCodeGTMKit
 */
const nullTo = function(value, defaultValue) {
    return value === null ? defaultValue : value;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// nullTo - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(nullTo(value, data.def));
// ===============================================================================
// nullTo(...) – Apply Mode
// ===============================================================================
/*
return function(value, defaultValue) {
   defaultValue = data.rp1 ? data.def : defaultValue;
   return out(nullTo(value, defaultValue));
};
*/
🧪 View Test Scenarios (5 tests)
✅ Undefined value - returns original value
✅ '[example] String passes through'
✅ Zero value - returns original zero value
✅ '[example] Null returns default'
✅ False value - returns original false value