π§© IF ELSE IF (Predefined) β GTM Variable Template for Logic
π§© IF ELSE IF (Predefined) EXTENDED Logic
IF-ELSE-IF logic with predefined comparison operators (equals, contains, starts with, regex, numeric). Returns first match or default value.
Examples
Section titled βExamplesβEquals match
INPUT
Params Table: [
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "hello",
"Output Value": "Match 1"
},
{
"Input Value": "world",
"Condition": "eq",
"Reference Value": "world",
"Output Value": "Match 2"
}
]
Default Output: No match
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "hello",
"Output Value": "Match 1"
},
{
"Input Value": "world",
"Condition": "eq",
"Reference Value": "world",
"Output Value": "Match 2"
}
]
Default Output: No match
OUTPUT
Match 1
No match returns default
INPUT
Params Table: [
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "goodbye",
"Output Value": "Match 1"
},
{
"Input Value": "world",
"Condition": "eq",
"Reference Value": "earth",
"Output Value": "Match 2"
}
]
Default Output: Default value
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "goodbye",
"Output Value": "Match 1"
},
{
"Input Value": "world",
"Condition": "eq",
"Reference Value": "earth",
"Output Value": "Match 2"
}
]
Default Output: Default value
OUTPUT
Default value
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 (Predefined)
IF ELSE β¬
Params Table
π‘ Tips:
β’ Order matters! Conditions are checked from top to bottom
β’ First match wins - remaining conditions are skipped
β’ Use specific conditions first, general conditions last
β’ Combine operators:
β’ Order matters! Conditions are checked from top to bottom
β’ First match wins - remaining conditions are skipped
β’ Use specific conditions first, general conditions last
β’ Combine operators:
starts with (case insensitive) for flexible matchingInput ValueConditionReference ValueOutput Value
β
β
ELSE β¬
Default Output
π€ The value to return if none of the conditions match.
This is the "ELSE" in the IF-ELSE-IF-ELSE logic.
If left empty, returns
Examples:
β’
β’
β’
This is the "ELSE" in the IF-ELSE-IF-ELSE logic.
If left empty, returns
undefined.Examples:
β’
Unknownβ’
0β’
{{Fallback Variable}}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 chaining transformations or 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 chaining transformations or ensuring consistent output format.
Params Table table
Input ValueConditionReference ValueOutput Value
Default Output string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
ifElseIf()
Related Variables
Section titled βRelated VariablesβSame category: Logic
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Evaluates conditions sequentially using IF ELSE IF logic. * Returns the value of the first matching condition, or default if none match. * * @param {Array<Object>} ls1 - Array of condition objects with values to return. * @param {*} def - Default value to return if no conditions match. * * @returns {*} The value of the first matching condition, or default value. * * @framework ggLowCodeGTMKit */const makeNumber = require('makeNumber');
const ifElseIf = function(ls1, def) { const toStr = function(val) { if (val === null) return 'null'; if (val === undefined) return 'undefined'; return typeof val === 'string' ? val : val.toString(); };
const toLower = function(str) { return str.toLowerCase(); };
const convertToNumber = function(value) { if (typeof value === 'number') return value; if (typeof value !== 'string') return value; const num = makeNumber(value); return (typeof num === 'number' && num === num) ? num : value; };
const ruleMethods = { eq: function(val, ref) { return val == ref; }, ct: function(val, ref) { return val.indexOf(ref) > -1; }, sw: function(val, ref) { return val.substring(0, ref.length) === ref; }, ew: function(val, ref) { return val.substring(val.length - ref.length) === ref; }, re: function(val, ref) { return val.search(ref) !== -1; }, xlt: function(val, ref) { return val < ref; }, xlte: function(val, ref) { return val <= ref; } };
const evaluateCondition = function(cond) { if (!cond) return false;
const condKey = cond.con || ''; if (condKey.length === 0) return false;
const firstChar = condKey[0]; const lastChar = condKey[condKey.length - 1];
const isNegated = firstChar === 'n' && condKey.length > 1; const isInsensitive = lastChar === 'i' && condKey.length > 1;
const baseStart = isNegated ? 1 : 0; const baseEnd = isInsensitive ? condKey.length - 1 : condKey.length; const baseCondition = condKey.substring(baseStart, baseEnd);
const ruleFn = ruleMethods[baseCondition]; if (!ruleFn) return false;
var val = cond.val; var ref = cond.ref;
const isNumericRule = baseCondition[0] === 'x';
if (isNumericRule) { val = convertToNumber(val); ref = convertToNumber(ref); } else { val = toStr(val); ref = toStr(ref);
if (isInsensitive) { val = toLower(val); ref = toLower(ref); } }
const result = ruleFn(val, ref); return isNegated ? !result : result; };
// Sequential IF-ELSE-IF evaluation const rules = ls1 || []; for (var i = 0; i < rules.length; i++) { var rule = rules[i]; if (!rule) continue;
// Support both nested {condition: {val, con, ref}, res} and flat {val, con, refπ§ͺ View Test Scenarios (27 tests)
β
'[example] Equals match'β
Second condition matches (first fails)β
'[example] No match returns default'β
Case insensitive equals (eqi)β
Contains (ct)β
Not contains (nct)β
Starts with (sw)β
Ends with case insensitive (ewi)β
Regex match (re)β
Numeric less than (xlt)β
Numeric not less than (nxlt = greater than or equal)β
Multiple conditions with priority (first match wins)β
With output functionβ
Empty rules array returns defaultβ
Invalid condition (missing operator) skips to nextβ
null is stringified for string comparisonβ
undefinedis stringified for string comparisonβ
boolean true is stringified for string comparisonβ
boolean false is stringified for string comparisonβ
number becomes string for string comparisonβ
null in contains operationβ
String 10 vs number 10 in numeric comparison (both converted)β
Number in string operation (toString called)β
Boolean in contains (true β string true)β
Numeric comparison with non-numeric string (stays as is)β
Reference value stringification (ref with boolean)β
Both val and ref are null