π§© IF (else) β GTM Variable Template for Logic
π§© IF (else) CORE Logic
Returns one of two values based on a boolean condition flag.
Examples
Section titled β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
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.
π§© 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
Section titled βRelated VariablesβSame category: Logic
Under the Hood
Section titled β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