Skip to content

applyChain β€” GTM Variable Template for Value

VARIABLES β€Ί VALUE
applyChain CORE Value
Direct (.tpl) Apply (.tpl)

Applies a chain of functions sequentially to a value.


Value Utilities

Handle edge cases β€” defaults for undefined/null, type checks, coercion.


Single function chain
INPUT
Input Value: 10
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}]
OUTPUT
4

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
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
applyChain()


πŸ“œ 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