Skip to content

falsyTo β€” GTM Variable Template for Value

VARIABLES β€Ί VALUE
falsyTo CORE Value
Direct (.tpl) Apply (.tpl)

Replaces falsy values (false, 0, "", null, undefined, NaN) with a default value. Returns the original if truthy, otherwise returns the default.


Conditional Logic

Branch logic based on conditions β€” if/else, ternary, boolean gates.


Truthy value returns original
INPUT
Value To Check: hello
Default Value: default
OUTPUT
hello
Empty string returns default
INPUT
Value To Check:
Default Value: default
OUTPUT
default
Null returns default
INPUT
Value To Check: null
Default Value: fallback
OUTPUT
fallback

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

falsyTo
Value To Check
πŸ’Ύ The value to check for falsy values.

Supported formats:
  βœ“ Any
Default Value
πŸ’Ύ The default value to return if the source value is falsy.

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


πŸ“œ View Implementation Code
/**
* Replaces falsy values 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 falsy.
* @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 truthy, otherwise returns the default value.
*
* @framework ggLowCodeGTMKit
*/
const falsyTo = function(value, defaultValue) {
return value || defaultValue;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// falsyTo - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(falsyTo(value, data.def));
// ===============================================================================
// falsyTo(...) – Apply Mode
// ===============================================================================
/*
return function(value, defaultValue) {
defaultValue = data.rp1 ? defaultValue : data.def;
return out(falsyTo(value, defaultValue));
};
*/
πŸ§ͺ View Test Scenarios (10 tests)
βœ… '[example] Truthy value returns original'
βœ… '[example] Empty string returns default'
βœ… '[example] Null returns default'
βœ… Falsy value (undefined) - should return default
βœ… Falsy value (0) - should return default
βœ… Falsy value (false) - should return default
βœ… Truthy number - should return original
βœ… Truthy object - should return original
βœ… Empty array (truthy) - should return original
βœ… String with spaces (truthy) - should return original