Skip to content

🧩 IF AND/OR β€” GTM Variable Template for Logic

VARIABLES β€Ί LOGIC
🧩 IF AND/OR CORE Logic
Direct (.tpl)

Applies AND if condition is true, OR if false.



AND all truthy
INPUT
Condition to decide which logic to apply: true
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'}]
OUTPUT
first truthy

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/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
ifAndOr()


πŸ“œ 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