Skip to content

subtractValues β€” GTM Variable Template for Number

VARIABLES β€Ί NUMBER
subtractValues EXTENDED Number
Direct (.tpl) Apply (.tpl)

Subtracts one number from another. Returns undefined if invalid.



Simple subtraction
INPUT
Minuend (From): 10
Subtrahend (Amount): 3
OUTPUT
7
String numbers subtracted
INPUT
Minuend (From): 20
Subtrahend (Amount): 8
OUTPUT
12
Invalid returns undefined
INPUT
Minuend (From): hello
Subtrahend (Amount): 5
OUTPUT
undefined

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
Subtrahend (Amount)
πŸ’Ύ The number to subtract (amount to remove).

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


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