percentOf — GTM Variable Template for Number
percentOf EXTENDED Number
Calculates what percentage the first value is of the second value. Formula: (Value / Total Value) × 100.
When to Use This
Section titled “When to Use This”Number Operations
Arithmetic, rounding, clamping, and mathematical transformations on numeric values.
Examples
Section titled “Examples”Calculate percentage
INPUT
Value: 25
Total Value: 100
Total Value: 100
OUTPUT
25
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.
percentOf
Value
💾 The value you want to find the percentage for.
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ String
Total Value
💾 The total or base value to compare against.
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.
Value number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Total Value number
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
percentOf()
Related Variables
Section titled “Related Variables”Same category: Number
Under the Hood
Section titled “Under the Hood”📜 View Implementation Code
/*** Calculates what percentage the first value is of the second value.** @param {number|string} data.src - The value to find the percentage for.* @param {number|string} data.val - The total value.* @param {Function|string} [data.out] - Optional output handler.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src before calculation.** @returns {number} The calculated percentage, or 0 if inputs are invalid or total is 0.** @framework ggLowCodeGTMKit*/const Math = require('Math');const makeNumber = require('makeNumber');
const percentOf = function(value, totalValue) { const val = makeNumber(value); const tot = makeNumber(totalValue); if (val !== val || tot !== tot) return 0; // Guard against NaN inputs if (tot === 0) return 0; return Math.round((val / tot) * 100);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// percentOf - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(percentOf(value, data.val));// ===============================================================================// percentOf(...) – Apply Mode// ===============================================================================/*return function(value, totalValue) { totalValue = data.rp1 ? totalValue : data.val; return out(percentOf(value, totalValue));};*/🧪 View Test Scenarios (5 tests)
✅ '[example] Calculate percentage'✅ String numbers - converts and calculates✅ Returns 0 if total is 0✅ Rounds to nearest integer✅ Invalid input - returns 0