๐งฉ XOR โ GTM Variable Template for 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}
]
{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'}
]
{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
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
xor()
Related Variables
Same category: Logic
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