Skip to content

scaleToRange β€” GTM Variable Template for Number

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

Scales a value from an original range to a new target range.


Number Operations

Arithmetic, rounding, clamping, and mathematical transformations on numeric values.

Type Conversion

Safely convert between data types β€” strings, numbers, booleans, arrays, objects.


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
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
OUTPUT
10

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
Original Minimum
πŸ“‰ The lower bound of the original range.

Supported formats:
  βœ“ Number
  βœ“ String
Original Maximum
πŸ“ˆ The upper bound of the original range.

Supported formats:
  βœ“ Number
  βœ“ String
Target Minimum
πŸ“‰ The lower bound of the new target range.

Supported formats:
  βœ“ Number
  βœ“ String
Target Maximum
πŸ“ˆ The upper bound of the new target range.

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


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