Skip to content

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

VARIABLES โ€บ 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
and()


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