Skip to content

🧩 AND β€” GTM Variable Template for Logic

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

Checks the provided list of parameters and returns the first falsy value it encounters. If all values are truthy, it returns the last truthy value, mimicking the logical AND


Logic Operations

Boolean algebra β€” AND, OR, NOT, XOR for combining conditions.

Comparison

Test equality, containment, and ordering between values.

URL Processing

Parse, build, decode, and manipulate URLs and query parameters.


All truthy returns last
INPUT
Values To Test : [ {val: true}, {val: 'hello'}, {val: 42}, {val: 'last'}]
OUTPUT
last
First falsy returned
INPUT
Values To Test : [{val: true}, {val: 0}, {val: 'hello'}, {val: 'last'}]
OUTPUT
0

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

🧩 AND
AND ⬇
Values To Test (Return last)
βŠ–
Input Setup
Input Function (optional)
Optional pre-processing function applied to each value before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
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.
Values To Test (Return last) list
and()


πŸ“œ View Implementation Code
/**
* Returns the first **falsy** value found, or the **last value** if all are truthy
* (mimics && behavior exactly).
*
* @param {Array<Object>} ls1 - List of parameter objects to evaluate.
* @param {*} ls1[].val - Value to evaluate in the logical AND chain
*
* @returns {*} First falsy value found, or last value if no falsy found.
*
* @framework ggLowCodeGTMKit
*/
const and = function(ls1) {
let lastValue;
for (const parameter of ls1) {
if (!parameter) {
return false;
}
if (!parameter.val) {
return parameter.val;
}
lastValue = parameter.val;
}
return lastValue;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const out = safeFunction(data.out);
// ===============================================================================
// and - Direct mode
// ===============================================================================
const values = (data.ls1 || []).map(item => ({val: applyCast(data.pre, item ? item.val : undefined)}));
return out(and(values));
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] All truthy returns last'
βœ… '[example] First falsy returned'
βœ… Test empty string is falsy and returned
βœ… Test with pre function that transforms values
βœ… Test with pre function that makes one value falsy