π§© 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
Section titled β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
Section titled β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
Section titled βRelated VariablesβSame category: Logic
Under the Hood
Section titled β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