calculateRatioPercentage — GTM Variable Template for Number
calculateRatioPercentage EXTENDED Number
Calculates the percentage ratio. Formula: (firstValue / (secondValue - firstValue)) × 100.
Examples
Calculate ratio percentage
INPUT
First Value: 25
Second Value: 100
Second Value: 100
OUTPUT
33
Equal values return 0
INPUT
First Value: 100
Second Value: 100
Second Value: 100
OUTPUT
0
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
calculateRatioPercentage
First Value
💾 The first value (base value for ratio calculation).
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ String
Second Value
💾 The second value (comparison value).
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
First Value number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Second Value number
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
calculateRatioPercentage()
Related Variables
Same category: Number
Under the Hood
📜 View Implementation Code
/**
* Calculates the percentage ratio of the first value to the difference between two values.
*
* @param {number|string} data.src - The first value (base value).
* @param {number|string} data.val - The second value (comparison value).
* @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 src before calculation.
*
* @returns {number} Rounded percentage from value difference, or 0 if invalid.
*
* @framework ggLowCodeGTMKit
*/
const Math = require('Math');
const makeNumber = require('makeNumber');
const calculateRatioPercentage = function(firstValue, secondValue) {
const val = makeNumber(firstValue);
const val = makeNumber(secondValue);
if (val !== val || val !== val) return 0; // Guard against NaN inputs
if (val <= val) return 0;
return Math.round((100 * val) / (val - val));
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// calculateRatioPercentage - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(calculateRatioPercentage(value, data.val));
// ===============================================================================
// calculateRatioPercentage(...) – Apply Mode
// ===============================================================================
/*
return function(value, secondValue) {
secondValue = data.rp1 ? data.val : secondValue;
return out(calculateRatioPercentage(value, secondValue));
};
*/🧪 View Test Scenarios (6 tests)
✅ '[example] Calculate ratio percentage'
✅ String numbers - converts and calculates
✅ Second value less than or equal to first - returns 0
✅ '[example] Equal values return 0'
✅ Invalid input - returns 0
✅ Invalid inputs - returns 0