Skip to content

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

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

Checks a list of parameters and returns the first truthy value. If no truthy value is found, it returns false, mimicking the logical OR (||) operator.


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

First truthy returned
INPUT
Values To Test: [
{val: false},
{val: 0},
{val: 'first truthy'},
{val: 'second truthy'}
]
OUTPUT
first truthy
All falsy returns false
INPUT
Values To Test: [
{val: false},
{val: 0},
{val: ''},
{val: null},
{val: undefined}
]
OUTPUT
false

GTM Configuration

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

๐Ÿงฉ OR
OR โฌ‡
Values To Test (Return first truthy)
โŠ–
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 first truthy) list
or()


Under the Hood

๐Ÿ“œ View Implementation Code
/**
 * Returns the first **truthy** `val` found in a list of parameter objects.
 *
 * @param {Array<Object>} ls1 - List of parameter objects to evaluate.
 * @param {*} ls1[].val - Value to evaluate for truthiness
 *
 * @returns {*} The first truthy value (`val`) found in the list, or `false` if none is found.
 *
 * @framework ggLowCodeGTMKit
 */
const or = function(ls1) {
	for (const parameter of ls1) {
		if (parameter && parameter.val) {
			return parameter.val;
		}
	}
	return false;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const out = safeFunction(data.out);
// ===============================================================================
// or - Direct mode
// ===============================================================================
const values = (data.ls1 || []).map(item => ({val: applyCast(data.pre, item ? item.val : undefined)}));
return out(or(values));
๐Ÿงช View Test Scenarios (5 tests)
โœ… '[example] First truthy returned'
โœ… '[example] All falsy returns false'
โœ… Test first value truthy returns immediately
โœ… Test with pre function that transforms values
โœ… Test with pre function where all values fail condition