clamp β GTM Variable Template for Number
clamp CORE Number
Clamps a number within a specified range defined by min and max values.
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βWithin range unchanged
INPUT
Value To Clamp: 5
Minimum Value: 0
Maximum Value: 10
Minimum Value: 0
Maximum Value: 10
OUTPUT
5
Below minimum clamped up
INPUT
Value To Clamp: -5
Minimum Value: 0
Maximum Value: 10
Minimum Value: 0
Maximum Value: 10
OUTPUT
0
Above maximum clamped down
INPUT
Value To Clamp: 15
Minimum Value: 0
Maximum Value: 10
Minimum Value: 0
Maximum Value: 10
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.
clamp
Value To Clamp
πΎ The number to be clamped within the specified range.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Minimum Value
π The lower bound of the range.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Maximum Value
π The upper bound of the range.
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 To Clamp number
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Minimum Value number
Maximum Value number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
clamp()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Clamps a number within a specified range defined by the min and max values.** @param {number} data.src - The number to be clamped.* @param {number|string} data.min - The lower bound of the range.* @param {number|string} data.max - The upper bound of the range.* @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 clamping.** @returns {number} The clamped number, constrained between min and max.** @framework ggLowCodeGTMKit*/const Math = require('Math');const makeNumber = require('makeNumber');
const clamp = function(value, min, max) { const val = makeNumber(value); const minVal = makeNumber(min); const maxVal = makeNumber(max); return Math.max(minVal, Math.min(val, maxVal));};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// clamp - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(clamp(value, data.min, data.max));
// ===============================================================================// clamp(...) β Apply Mode: UI-configured parameter// ===============================================================================/*return function(value, min, max) { min = data.rp1 ? min : data.min; max = data.rp2 ? max : data.max; return out(clamp(value, min, max));};*/π§ͺ View Test Scenarios (8 tests)
β
'[example] Within range unchanged'β
'[example] Below minimum clamped up'β
'[example] Above maximum clamped down'β
Clamp value at minimum boundaryβ
Clamp value at maximum boundaryβ
Clamp negative rangeβ
Clamp decimal valueβ
String number values