Skip to content

๐Ÿงฉ IF (else) โ€” GTM Variable Template for Logic

VARIABLES โ€บ 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
False returns default
INPUT
Condition: false
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
ifElse()


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