filterValue — GTM Variable Template for Value
filterValue CORE Value
Tests a value with a callback function and returns the value if it passes the test, otherwise returns undefined. Useful for conditional value filtering in function chains.
When to Use This
Value Utilities
Handle edge cases — defaults for undefined/null, type checks, coercion.
Filtering
Select or exclude items from collections based on criteria or predicates.
Comparison
Test equality, containment, and ordering between values.
Examples
Value passes filter
INPUT
Value To Test: 42
Test Function: x => typeof x === 'number'
Test Function: x => typeof x === 'number'
OUTPUT
42
Value fails filter
INPUT
Value To Test: hello
Test Function: x => typeof x === 'number'
Test Function: x => typeof x === 'number'
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
filterValue
Value To Test
💾 The value to test with the callback function.
Supported formats:
✓ Any type (string, number, object, array, etc.)
Supported formats:
✓ Any type (string, number, object, array, etc.)
Test Function
Returns the value if it passes a test function, otherwise returns undefined.
*** Value passes filter***
Input: Value To Test: 42
Test Function: x => typeof x === 'number'
↪️ Output: 42
*** Value fails filter***
Input: Value To Test: hello
Test Function: x => typeof x === 'number'
↪️ Output: undefined
*** Value passes filter***
Input: Value To Test: 42
Test Function: x => typeof x === 'number'
↪️ Output: 42
*** Value fails filter***
Input: Value To Test: hello
Test Function: x => typeof x === 'number'
↪️ Output: undefined
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the value before testing (e.g., normalize format, convert types). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., x => x * 2 to double the value, x => x || 'default' to provide fallback). Useful for chaining transformations on the output.
Value To Test number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Test Function function
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
filterValue()
Related Variables
Same category: Value
Under the Hood
📜 View Implementation Code
/**
* Tests a single value using a callback function and returns the value if it passes.
*
* @param {*} data.src - The value to test.
* @param {Function} data.cbk - A function that receives the value and returns true to keep it, false to reject it.
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before testing.
*
* @returns {*|undefined} The original value if the callback returns true, otherwise undefined.
*
* @framework ggLowCodeGTMKit
*/
const filterValue = function(value, callback) {
if (typeof callback !== 'function') {
return undefined;
}
return callback(value) ? value : undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// filterValue - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedValue = applyCast(data.pre, data.src);
return out(filterValue(processedValue, data.cbk));
// ===============================================================================
// filterValue(...) – Apply Mode
// ===============================================================================
/*
return function(value, callback) {
callback = data.rp1 ? data.cbk : callback;
return out(filterValue(value, callback));
};
*/🧪 View Test Scenarios (6 tests)
✅ '[example] Value passes filter'
✅ '[example] Value fails filter'
✅ Value passes test - greater than threshold
✅ Undefined value check - passes
✅ Undefined value check - fails
✅ Invalid callback (not a function)