mapTruthiness β GTM Variable Template for Value
mapTruthiness CORE Value
Maps truthy values to one output and falsy values to another output.
When to Use This
Section titled βWhen to Use ThisβConditional Logic
Branch logic based on conditions β if/else, ternary, boolean gates.
Examples
Section titled βExamplesβTruthy maps to truthy value
INPUT
Predicate Value: hello
Truthy Value: 1
Falsy Value: 0
Truthy Value: 1
Falsy Value: 0
OUTPUT
1
Falsy maps to falsy value
INPUT
Predicate Value:
Truthy Value: success
Falsy Value: failure
Truthy Value: success
Falsy Value: failure
OUTPUT
failure
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.
mapTruthiness
Predicate Value
πΎ The predicate value to evaluate for truthiness.
Supported formats:
β Any
Supported formats:
β Any
Truthy Value
πΎ The value to return when predicate is truthy.
Supported formats:
β Any
Supported formats:
β Any
Falsy Value
πΎ The value to return when predicate is falsy.
Supported formats:
β Any
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
mapTruthiness()
Related Variables
Section titled βRelated VariablesβSame category: Value
Under the Hood
Section titled β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 ? truthyValue : data.tru; falsyValue = data.rp2 ? falsyValue : data.fal; 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