subtractValues β GTM Variable Template for Number
subtractValues EXTENDED Number
Subtracts one number from another. Returns undefined if invalid.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβSimple subtraction
INPUT
Minuend (From): 10
Subtrahend (Amount): 3
Subtrahend (Amount): 3
OUTPUT
7
String numbers subtracted
INPUT
Minuend (From): 20
Subtrahend (Amount): 8
Subtrahend (Amount): 8
OUTPUT
12
Invalid returns undefined
INPUT
Minuend (From): hello
Subtrahend (Amount): 5
Subtrahend (Amount): 5
OUTPUT
undefined
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.
subtractValues
Minuend (From)
πΎ The number to subtract from (starting value).
Supported formats:
β Number
Supported formats:
β Number
Subtrahend (Amount)
πΎ The number to subtract (amount to remove).
Supported formats:
β Number
Supported formats:
β Number
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the minuend before internal logic (e.g., parse string, apply transformations). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., val => Math.round(val), val => val.toFixed(2) for decimal places). Useful for chaining transformations on the output.
Minuend (From) number
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Subtrahend (Amount) number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
subtractValues()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Subtracts the subtrahend from the minuend and returns the result. * * @param {*} data.min - The number to subtract from (the starting value). * @param {*} data.sub - The number to subtract (the amount to remove). * @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 min before calculation. * * @returns {number|undefined} The result of the subtraction, or undefined if either input is not a valid number. * * @framework ggLowCodeGTMKit */const makeNumber = require('makeNumber');
const subtractValues = function(minuend, subtrahend) { const min = makeNumber(minuend); const sub = makeNumber(subtrahend); if (min === min && sub === sub) { return min - sub; } return undefined;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// subtractValues - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedMinuend = applyCast(data.pre, data.min);return out(subtractValues(processedMinuend, data.sub));// ===============================================================================// subtractValues(...) β Apply Mode// ===============================================================================/*return function(value, subtrahend) { subtrahend = data.rp1 ? subtrahend : data.sub; return out(subtractValues(value, subtrahend));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Simple subtraction'β
Negative result - should return negative differenceβ
'[example] String numbers subtracted'β
Decimal numbers - should handle decimalsβ
'[example] Invalid returns undefined'