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
Number Operations
Arithmetic, rounding, clamping, and mathematical transformations on numeric values.
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
GTM Configuration
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
Same category: Number
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) {
return out(clamp(value, data.min, data.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