apply — GTM Variable Template for Value
When to Use This
Value Utilities
Handle edge cases — defaults for undefined/null, type checks, coercion.
Examples
Apply function to number
INPUT
Input Value: 10
Function: x => x * 2
Function: x => x * 2
OUTPUT
20
Invalid function returns undefined
INPUT
Input Value: 42
Function: not a function
Function: not a function
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
apply
Input Value
💾 The value that will be passed to the function.
Supported formats:
✓ Any
Supported formats:
✓ Any
Function
⚙️ The function that will be applied to the input value.
Supported formats:
✓ Function
Supported formats:
✓ Function
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input 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 + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Input Value any
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Function function
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
apply()
Related Variables
Same category: Value
Under the Hood
📜 View Implementation Code
/**
* Applies a function to the input value and returns the result.
*
* @param {*} data.src - The value that will be passed to the function.
* @param {function} data.fnc - The function that will be applied to the input value.
* @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 processing.
*
* @returns {*} The result of applying the function to the input value, or undefined if the function is not provided or not a valid function.
*
* @framework ggLowCodeGTMKit
*/
const apply = function(input, func) {
if (typeof func !== 'function') { return undefined; }
return func(input);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// apply - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedInput = applyCast(data.pre, data.src);
return out(apply(processedInput, data.fnc));🧪 View Test Scenarios (5 tests)
✅ '[example] Apply function to number'
✅ Valid function applied to string - returns transformed string
✅ Valid function applied to array - returns array length
✅ '[example] Invalid function returns undefined'
✅ Undefined function - returns undefined