Skip to content

๐Ÿงฉ IF AND/OR โ€” GTM Variable Template for Logic

VARIABLES โ€บ LOGIC
๐Ÿงฉ IF AND/OR CORE Logic

Applies AND if condition is true, OR if false.



Examples

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

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


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