๐งฉ IF ELSE IF (Advanced) โ GTM Variable Template for 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
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
{ 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
โข 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 functionsInput ValueCustom FunctionReference ValueOutput Value
โ
โ
ELSE โฌ
Default Output
๐ค The value to return if none of the conditions match (all custom functions return
This is the "ELSE" in the IF-ELSE-IF-ELSE logic.
If left empty, returns
Examples:
โข
โข
โข
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:
Examples:
โข
โข
โข
โข
โข
๐ก If not provided, values are passed to custom functions unchanged.
Applied consistently to all conditions for normalization.
Function signature:
(value) => transformedValueExamples:
โข
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:
โข
โข
โข
โข
Useful for ensuring consistent output format.
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 valuesUseful for ensuring consistent output format.
Related Variables
Same category: Logic
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