Skip to content

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

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

Checks the provided list of parameters and returns the single truthy value if exactly one is found, false otherwise, mimicking the logical XOR 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

One truthy returns value
INPUT
Values to test: [
{val: false},
{val: 0},
{val: 'only truthy'},
{val: ''},
{val: null}
]
OUTPUT
only truthy
Two truthy returns false
INPUT
Values to test: [
{val: 'first'},
{val: false},
{val: 'second'}
]
OUTPUT
false

GTM Configuration

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

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


Under the Hood

๐Ÿ“œ View Implementation Code
/**
 * Returns the single truthy value if exactly one is found, false otherwise
 * (XOR logic - exactly one must be true)
 *
 * @param {Array<Object>} ls1 - List of parameter objects to evaluate.
 * @param {*} ls1[].val - Value to evaluate in the XOR logic
 *
 * @returns {*} Single truthy value if exactly one is found, false otherwise
 *
 * @framework ggLowCodeGTMKit
 */
const xor = function(ls1) {
	let foundValue = null;
	let foundCount = 0;
	for (const parameter of ls1) {
		if (!parameter) continue;
		if (parameter.val) {
			foundCount++;
			if (foundCount === 1) {
				foundValue = parameter.val;
			} else {
				return false;
			}
		}
	}
	return foundCount === 1 ? foundValue : false;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const out = safeFunction(data.out);
// ===============================================================================
// xor - Direct mode
// ===============================================================================
const values = (data.ls1 || []).map(item => item ? {val: applyCast(data.pre, item.val)} : item);
return out(xor(values));
๐Ÿงช View Test Scenarios (5 tests)
โœ… '[example] One truthy returns value'
โœ… '[example] Two truthy returns false'
โœ… Test all falsy values returns false
โœ… Test with pre function where exactly one passes
โœ… Test with pre function where multiple pass returns false