Skip to content

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

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

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


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

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.


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