applyChain β GTM Variable Template for Value
applyChain CORE Value
Applies a chain of functions sequentially to a value.
When to Use This
Section titled βWhen to Use ThisβValue Utilities
Handle edge cases β defaults for undefined/null, type checks, coercion.
Examples
Section titled βExamplesβSingle function chain
INPUT
Input Value: 10
Functions: [{func: x => x * 2}]
Functions: [{func: x => x * 2}]
OUTPUT
20
Multiple function chain
INPUT
Input Value: 5
Functions: [{func: x => x * 2}, {func: x => x + 10}, {func: x => x / 5}]
Functions: [{func: x => x * 2}, {func: x => x + 10}, {func: x => x / 5}]
OUTPUT
4
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
applyChain
Input Value
πΎ The initial value to which the functions will be applied.
Supported formats:
β Any
Supported formats:
β Any
Functions
β
Input Value any
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Functions list
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
applyChain()
Related Variables
Section titled βRelated VariablesβSame category: Value
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Applies a list of functions sequentially to an input value.** @param {*} data.src - The initial value to which the functions will be applied.* @param {Array} data.lst - An array of function objects to be applied sequentially.** @returns {*} The result after all functions have been applied to the input.** @framework ggLowCodeGTMKit*/
const applyChain = function(input, functionList) { return functionList.reduce((acc, fnObj) => { if (typeof fnObj.func === 'function') { return fnObj.func(acc); } return acc; }, input);};
// ===============================================================================// applyChain - Direct mode only// ===============================================================================return applyChain(data.src, data.lst);
// ===============================================================================// applyChain(...) β Apply Mode// ===============================================================================/*return function(input) { return applyChain(input, data.lst) ;};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Single function chain'β
'[example] Multiple function chain'β
Empty function list returns original valueβ
Function list with invalid function object - skips invalid and continuesβ
String transformation chain - applies string operations