Skip to content

applyChain — GTM Variable Template for Value

VARIABLES › VALUE
applyChain CORE Value

Applies a chain of functions sequentially to a value.


When to Use This

Value Utilities

Handle edge cases — defaults for undefined/null, type checks, coercion.


Examples

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

GTM Configuration

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


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