๐งฉ 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
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
GTM Configuration
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
Same category: Logic
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