nullTo β GTM Variable Template for Value
nullTo CORE Value
Replaces null values with a default. Non-null values pass through unchanged.
When to Use This
Section titled βWhen to Use ThisβValue Utilities
Handle edge cases β defaults for undefined/null, type checks, coercion.
Examples
Section titled βExamplesβString passes through
INPUT
Value To Check: hello
Default Value: default
Default Value: default
OUTPUT
hello
Null returns default
INPUT
Value To Check: null
Default Value: default
Default Value: default
OUTPUT
default
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.
nullTo
Value To Check
πΎ The value to check for null.
Supported formats:
β Any
Supported formats:
β Any
Default Value
πΎ The default value to return if the source value is null.
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 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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
nullTo()
Related Variables
Section titled βRelated VariablesβSame category: Value
Under the Hood
Section titled β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 ? defaultValue : data.def; 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