๐งฉ IF AND/OR โ GTM Variable Template for Logic
Examples
AND all truthy
INPUT
Condition to decide which logic to apply: true
Values To Test: [{val: 'hello'}, {val: 'world'},{val: 'test'}]
Values To Test: [{val: 'hello'}, {val: 'world'},{val: 'test'}]
OUTPUT
test
OR first truthy
INPUT
Condition to decide which logic to apply: false
Values To Test: [{val: false}, {val: 'first truthy'}, {val: 'second truthy'}]
Values To Test: [{val: false}, {val: 'first truthy'}, {val: 'second truthy'}]
OUTPUT
first truthy
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
๐งฉ IF AND/OR
IF โฌ
Condition to decide which logic to apply
๐ Applies AND if the condition is true, otherwise applies OR.
โณ AND: Returns the first falsy value found, or the last value if all are truthy
โณ OR: Returns the first truthy value found or the last value if all are falsy.
โณ AND: Returns the first falsy value found, or the last value if all are truthy
โณ OR: Returns the first truthy value found or the last value if all are falsy.
AND/OR โฌ
Values To Test
โ
Result Handling
Output Function (optional)
Optional function to apply to the result before returning it (e.g., `str => str + ' โฌ'`). Useful for chaining transformations on the output.
Condition to decide which logic to apply string
๐ก Type any text to see the result update live
๐ฏ Using special value โ click input to type instead
Test with:
Falsy
Truthy
Values To Test list
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
ifAndOr()
Related Variables
Same category: Logic
Under the Hood
๐ View Implementation Code
/**
* Evaluates conditions using dynamic logic selection based on a flag.
* Switches between AND logic (true) and OR logic (false).
*
* @param {boolean} useAnd - Flag to determine logic type (true = AND, false = OR)
* @param {Array<Object>} ls1 - Array of value objects to evaluate
* @param {*} ls1[].val - Value to evaluate in the logic operation
*
* @returns {*} Result based on AND or OR logic.
*
* @framework ggLowCodeGTMKit
*/
const ifAndOr = function(useAnd, ls1) {
if (useAnd) {
// AND Logic: all must be truthy
let lastValue;
for (let i = 0; i < ls1.length; i++) {
const item = ls1[i];
if (!item) {
return false;
}
const value = item.val;
if (!value) {
return value;
}
lastValue = value;
}
return lastValue;
} else {
// OR Logic: first truthy wins
for (let i = 0; i < ls1.length; i++) {
const item = ls1[i];
if (!item) {
continue;
}
const value = item.val;
if (value) {
return value;
}
}
return false;
}
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// ifAndOr - Direct mode
// ===============================================================================
return out(ifAndOr(!!data.con, data.ls1 || []));๐งช View Test Scenarios (5 tests)
โ
'[example] AND all truthy'
โ
AND logic - one falsy value, returns first falsy
โ
'[example] OR first truthy'
โ
OR logic - all values are falsy, returns false
โ
AND logic with output function