Skip to content

subtractValues — GTM Variable Template for Number

VARIABLES › NUMBER
subtractValues EXTENDED Number

Subtracts one number from another. Returns undefined if invalid.



Examples

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

GTM Configuration

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


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 ? data.sub : subtrahend;
   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'