scaleToRange β GTM Variable Template for Number
scaleToRange EXTENDED Number
Scales a value from an original range to a new target range.
When to Use This
Section titled βWhen to Use ThisβNumber Operations
Arithmetic, rounding, clamping, and mathematical transformations on numeric values.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Examples
Section titled βExamplesβScale 0-100 to 0-1
INPUT
Value To Scale: 50
Original Minimum: 0
Original Maximum: 100
Target Minimum: 0
Target Maximum: 1
Clamp to Target Range: false
Original Minimum: 0
Original Maximum: 100
Target Minimum: 0
Target Maximum: 1
Clamp to Target Range: false
OUTPUT
0.5
Overflow with Clamp
INPUT
Value To Scale: 150
Original Minimum: 0
Original Maximum: 100
Target Minimum: 0
Target Maximum: 10
Clamp to Target Range: true
Original Minimum: 0
Original Maximum: 100
Target Minimum: 0
Target Maximum: 10
Clamp to Target Range: true
OUTPUT
10
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.
scaleToRange
Value To Scale
πΎ The value to be scaled.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Original Minimum
π The lower bound of the original range.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Original Maximum
π The upper bound of the original range.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Target Minimum
π The lower bound of the new target range.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Target Maximum
π The upper bound of the new target range.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
βοΈ Check this to prevent the result from exceeding the bounds of the target range if the input is outside the original range.
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 To Scale number
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Original Minimum number
Original Maximum number
Target Minimum number
Target Maximum number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
scaleToRange()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Scales a value from an original range to a new target range. * * @param {number|string} data.src - The value to be scaled. * @param {number|string} data.omin - The lower bound of the original range. * @param {number|string} data.omax - The upper bound of the original range. * @param {number|string} data.nmin - The lower bound of the new target range. * @param {number|string} data.nmax - The upper bound of the new target range. * @param {boolean} data.clmp - Whether to clamp the result to the target range. * @param {Function|string} [data.out] - Optional output handler. * * Direct-mode specific parameters: * @param {Function} [data.pre] - Optional pre-processor function to transform src. * * @returns {number|undefined} The scaled number. * * @framework ggLowCodeGTMKit */const Math = require('Math');const makeNumber = require('makeNumber');
const scaleToRange = function(value, oldMin, oldMax, newMin, newMax, shouldClamp) { const val = makeNumber(value); const oMin = makeNumber(oldMin); const oMax = makeNumber(oldMax); const nMin = makeNumber(newMin); const nMax = makeNumber(newMax);
if (val !== val || oMin !== oMin || oMax !== oMax || nMin !== nMin || nMax !== nMax) { return undefined; }
if (oMin === oMax) { return undefined; }
let result = ((val - oMin) / (oMax - oMin)) * (nMax - nMin) + nMin;
if (shouldClamp) { const minClamp = Math.min(nMin, nMax); const maxClamp = Math.max(nMin, nMax); if (result < minClamp) return minClamp; if (result > maxClamp) return maxClamp; }
return result;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// scaleToRange - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(scaleToRange(value, data.omin, data.omax, data.nmin, data.nmax, data.clmp));
// ===============================================================================// scaleToRange(...) β Apply Mode// ===============================================================================/*return function(value) { return out(scaleToRange(value, data.omin, data.omax, data.nmin, data.nmax, data.clmp));};*/π§ͺ View Test Scenarios (4 tests)
β
'[example] Scale 0-100 to 0-1'β
'[example] Overflow with Clamp'β
Overflow without Clampβ
Underflow with Clamp