calculateDiffRatio β GTM Variable Template for Number
calculateDiffRatio EXTENDED Number
Calculates the ratio of the difference between two values. Formula: (value - reference) / value.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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
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.
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
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled β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 ? reference : data.ref; 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