Skip to content

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

VARIABLES โ€บ LOGIC
๐Ÿงฉ IF ELSE IF (Advanced) CORE Logic

Evaluates a set of conditions and returns a value based on the first matching rule.


Examples

First match wins
INPUT
Conditions: [{

condition:
{
Input Value: 'hello',
Custom Function: (v, r) => v === r,
Reference: 'hello'
},
Result:
'Match 1'
},

{ condition:
{
Input Value: 'world',
Custom Function: (v, r) => v === r,
Reference: 'world'
},
Result:
'Match 2'
}
]
Default Output:
No match
OUTPUT
Match 1
No match returns default
INPUT
Conditions: [

{ condition:
{
Input Value: 'hello',
Custom Function: (v, r) => v === r,
Reference: 'goodbye'
},
Result:
'Match 1'
},

{ condition:
{
Input Value: 'world',
Custom Function: (v, r) => v === r,
Reference: 'xyz'
},
Result:
'Match 2'
}
]
Default Output:
Default value
OUTPUT
Default value

GTM Configuration

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

Read-only Preview
๐Ÿงฉ IF ELSE IF (Advanced)
IF ELSE โฌ‡
๐Ÿ’ก Tips:
โ€ข Order matters! Conditions are checked from top to bottom
โ€ข First match wins - remaining conditions are skipped
โ€ข Use specific conditions first, general conditions last
โ€ข Your custom functions can contain any JavaScript logic
โ€ข Access GTM APIs like require('makeNumber') inside functions
Input ValueCustom FunctionReference ValueOutput Value
โŠ–
โŠ–
ELSE โฌ‡
Default Output
๐Ÿ“ค The value to return if none of the conditions match (all custom functions return false).

This is the "ELSE" in the IF-ELSE-IF-ELSE logic.

If left empty, returns undefined.

Examples:
โ€ข Unknown
โ€ข 0
โ€ข {{Fallback Variable}}
Input Setup
Cast Function (optional)
โš™๏ธ Optional function to transform both val and ref before passing them to your custom functions.

Applied consistently to all conditions for normalization.

Function signature: (value) => transformedValue

Examples:
โ€ข v => String(v).toLowerCase() โ†’ Convert to lowercase
โ€ข v => String(v).trim() โ†’ Remove whitespace
โ€ข v => { const makeNumber = require('makeNumber'); return makeNumber(v) || v; } โ†’ Convert to number
โ€ข v => v && v.price ? v.price : 0 โ†’ Extract object property
โ€ข v => JSON.parse(v) โ†’ Parse JSON string

๐Ÿ’ก If not provided, values are passed to custom functions unchanged.
Result Handling
Output Function (optional)
โš™๏ธ Optional function to transform the final output before returning it.

Applied to both matched condition outputs and the default output.

Examples:
โ€ข str => str.toUpperCase() โ†’ Convert to uppercase
โ€ข val => val + ' โ‚ฌ' โ†’ Append currency
โ€ข val => parseInt(val) โ†’ Convert to integer
โ€ข val => val || 'N/A' โ†’ Fallback for empty values

Useful for ensuring consistent output format.


Under the Hood

๐Ÿ“œ View Implementation Code
/**
 * Evaluates conditions sequentially using custom functions (IF ELSE IF logic).
 * Returns the value of the first matching condition, or default if none match.
 *
 * Direct-mode only
 * @param {Array<Object>} data.ls1 - Array of condition objects with values to return
 * @param {Object} data.ls1[].condition - Condition object for this rule
 * @param {*} data.ls1[].condition.val - Value to evaluate
 * @param {Function} data.ls1[].condition.fn - Custom function that receives (val, ref) and returns boolean
 * @param {*} data.ls1[].condition.ref - Reference value to compare against
 * @param {*} data.ls1[].res - Value to return if this condition matches
 * @param {*} data.def - Default value to return if no conditions match
 * @param {Function} [data.cast] - Optional cast function applied to both val and ref before passing to custom function
 * @param {Function} [data.out] - Optional function to transform the final result
 *
 * @returns {*} The value of the first matching condition, or default value.
 *
 * @note Custom Functions Version - Use for complex logic not covered by predefined operators
 *
 * @framework ggLowCodeGTMKit
 */
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const castFn = safeFunction(data.cast);
const out = safeFunction(data.out);

const rules = data.ls1 || [];
const defaultValue = data.def;

const evaluateCondition = (cond) => {
    if (!cond) return false;
    
    const fn = cond.fn;
    if (typeof fn !== 'function') return false;
    
    const val = castFn(cond.val);
    const ref = castFn(cond.ref);
    
    return fn(val, ref);
};

for (let i = 0; i < rules.length; i++) {
    const rule = rules[i];
    if (!rule || !rule.condition) {
        continue;
    }
    
    if (evaluateCondition(rule.condition)) {
        return out(rule.res);
    }
}

return out(defaultValue);
๐Ÿงช View Test Scenarios (10 tests)
โœ… '[example] First match wins'
โœ… Second condition matches (first fails)
โœ… '[example] No match returns default'
โœ… Custom function with complex logic
โœ… With cast function - lowercase normalization
โœ… With cast function - numeric conversion
โœ… Custom function with object property comparison
โœ… With output function
โœ… Invalid function (not a function) - skips to next
โœ… Missing condition object - skips to next