Skip to content

mapTruthiness — GTM Variable Template for Value

VARIABLES › VALUE
mapTruthiness CORE Value

Maps truthy values to one output and falsy values to another output.


When to Use This

Conditional Logic

Branch logic based on conditions — if/else, ternary, boolean gates.


Examples

Truthy maps to truthy value
INPUT
Predicate Value: hello
Truthy Value: 1
Falsy Value: 0
OUTPUT
1
Falsy maps to falsy value
INPUT
Predicate Value:
Truthy Value: success
Falsy Value: failure
OUTPUT
failure

GTM Configuration

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

mapTruthiness
Predicate Value
💾 The predicate value to evaluate for truthiness.

Supported formats:
  ✓ Any
Truthy Value
💾 The value to return when predicate is truthy.

Supported formats:
  ✓ Any
Falsy Value
💾 The value to return when predicate is falsy.

Supported formats:
  ✓ Any
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the predicate 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.
Predicate Value any
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Truthy Value any
Falsy Value any
mapTruthiness()


Under the Hood

📜 View Implementation Code
/**
* Maps truthy values to one output and falsy values to another output.
* 
* @param {any} data.src - The input value to evaluate for truthiness.
* @param {any} data.tru - The value to return when the input is truthy.
* @param {any} data.fal - The value to return when the input 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 evaluation.
* 
* @returns {any} The truthy value if input is truthy, otherwise the falsy value.
*
* @framework ggLowCodeGTMKit
*/
const mapTruthiness = function(predicate, truthyValue, falsyValue) {
   return predicate ? truthyValue : falsyValue;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// mapTruthiness - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const predicate = applyCast(data.pre, data.src);
return out(mapTruthiness(predicate, data.tru, data.fal));
// ===============================================================================
// mapTruthiness(...) – Apply Mode
// ===============================================================================
/*
return function(value, truthyValue, falsyValue) {
   truthyValue = data.rp1 ? data.tru : truthyValue;
   falsyValue = data.rp2 ? data.fal : falsyValue;
   return out(mapTruthiness(value, truthyValue, falsyValue));
};
*/
🧪 View Test Scenarios (8 tests)
✅ Truthy value (true) - should return truthy value
✅ Falsy value (false) - should return falsy value
✅ '[example] Truthy maps to truthy value'
✅ '[example] Falsy maps to falsy value'
✅ Falsy value (0) - should return falsy value
✅ Truthy value (positive number) - should return truthy value
✅ Truthy value (empty object) - should return truthy value
✅ Truthy value (empty array) - should return truthy value