π§© IF AND/OR β GTM Variable Template for Logic
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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
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 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
Section titled βRelated VariablesβSame category: Logic
Under the Hood
Section titled β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