calculateRatioPercentage β GTM Variable Template for Number
calculateRatioPercentage EXTENDED Number
Calculates the percentage ratio. Formula: (firstValue / (secondValue - firstValue)) Γ 100.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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
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.
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
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled β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