Skip to content

calculateDiffRatio β€” GTM Variable Template for Number

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

Calculates the ratio of the difference between two values. Formula: (value - reference) / value.



Margin calculation
INPUT
Value: 100
Reference Value: 60
OUTPUT
0.4
Zero base returns undefined
INPUT
Value: 0
Reference Value: 60
OUTPUT
undefined

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}}
Reference Value
πŸ’Ύ The value to subtract from the base value (e.g., cost, sale price).

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


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