Skip to content

🧩 IF (else) β€” GTM Variable Template for Logic

VARIABLES β€Ί LOGIC
🧩 IF (else) CORE Logic
Direct (.tpl)

Returns one of two values based on a boolean condition flag.


True condition returns value
INPUT
Condition: true
Output: success
Default Output: failure
OUTPUT
success
False returns default
INPUT
Condition: false
Output: success
Default Output: failure
OUTPUT
failure

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


πŸ“œ 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