๐งฉ AND โ GTM Variable Template for Logic
๐งฉ AND CORE Logic
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
When to Use This
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.
Examples
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
GTM Configuration
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
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
and()
Related Variables
Same category: Logic
Under the Hood
๐ 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