๐งฉ IF (else) โ GTM Variable Template for Logic
๐งฉ IF (else) CORE Logic
Returns one of two values based on a boolean condition flag.
Examples
True condition returns value
INPUT
Condition: true
Output: success
Default Output: failure
Output: success
Default Output: failure
OUTPUT
success
False returns default
INPUT
Condition: false
Output: success
Default Output: failure
Output: success
Default Output: 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.
๐งฉ IF (else)
IF โฌ
Condition
THEN โฌ
Output
ELSE โฌ
Default Output
The value to return if none of the rules evaluate to true. Defaults to undefined.
Result Handling
Output Function (optional)
Optional function to apply to the result before returning it (e.g., `str => str + ' โฌ'`). Useful for chaining transformations on the output.
Condition string
๐ก Type any text to see the result update live
๐ฏ Using special value โ click input to type instead
Test with:
Falsy
Truthy
Output string
Default Output string
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
ifElse()
Related Variables
Same category: Logic
Under the Hood
๐ View Implementation Code
/**
* Evaluates a condition and returns one of two values (ternary operator).
* Returns result value if condition is true, or default value if false.
*
* @param {*} con - Condition to evaluate for truthiness.
* @param {*} res - Value to return when condition is true.
* @param {boolean} ael - Whether to use def value when condition is false (add else).
* @param {*} def - Value to return when condition is false (if ael is true).
*
* @returns {*} res value if condition is true, or def value (if ael is true) or undefined if false.
*
* @framework ggLowCodeGTMKit
*/
const ifElse = function(con, res, ael, def) {
const condition = !!con;
const output = ael ? def : undefined;
return condition ? res : output;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// ifElse - Direct mode
// ===============================================================================
return out(ifElse(data.con, data.res, data.ael, data.def));๐งช View Test Scenarios (5 tests)
โ
'[example] True condition returns value'
โ
'[example] False returns default'
โ
Test condition false with ael false returns undefined
โ
Test truthy value as condition
โ
Test falsy value as condition with default