calculateDiffRatio — GTM Variable Template for Number
calculateDiffRatio EXTENDED Number
Calculates the ratio of the difference between two values. Formula: (value - reference) / value.
Examples
Margin calculation
INPUT
Value: 100
Reference Value: 60
Reference Value: 60
OUTPUT
0.4
Zero base returns undefined
INPUT
Value: 0
Reference Value: 60
Reference Value: 60
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
calculateDiffRatio
Value
💾 The base value to use as the denominator and minuend (e.g., price, original value).
Supported formats:
✓ Number: 100
✓ Stringified Number: "100"
✓ Variable: {{price}}
Supported formats:
✓ Number: 100
✓ Stringified Number: "100"
✓ Variable: {{price}}
Reference Value
💾 The value to subtract from the base value (e.g., cost, sale price).
Supported formats:
✓ Number: 60
✓ Stringified Number: "60"
✓ Variable: {{cost}}
Supported formats:
✓ Number: 60
✓ Stringified Number: "60"
✓ Variable: {{cost}}
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the base value before calculating.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g.,
x => x * 100 to convert to percentage, x => (x * 100).toFixed(2) for formatted percentage).Value number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Reference Value number
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
calculateDiffRatio()
Related Variables
Same category: Number
Under the Hood
📜 View Implementation Code
/**
* Calculates the ratio of the difference between two values relative to the first value.
* Formula: (value - reference) / value
* Useful for margin, discount rate, and other proportional difference calculations.
*
* @param {number} data.src - The base value (e.g., price, current value).
* @param {number} data.ref - The reference value to subtract (e.g., cost, sale price).
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function.
*
* @returns {number|undefined} The ratio (src - ref) / src, or undefined if inputs are invalid.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const calculateDiffRatio = function(value, reference) {
const v = makeNumber(value);
const r = makeNumber(reference);
if (typeof v !== 'number' || v !== v) return undefined;
if (typeof r !== 'number' || r !== r) return undefined;
if (v === 0) return undefined;
return (v - r) / v;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// calculateDiffRatio - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(calculateDiffRatio(value, data.ref));
// ===============================================================================
// calculateDiffRatio(...) – Apply Mode
// ===============================================================================
/*
return function(value, reference) {
reference = data.rp1 ? data.ref : reference;
return out(calculateDiffRatio(value, reference));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Margin calculation'
✅ Discount rate
✅ Stringified numbers are converted
✅ '[example] Zero base returns undefined'
✅ Invalid src returns undefined