Skip to content

clamp β€” GTM Variable Template for Number

VARIABLES β€Ί NUMBER
clamp CORE Number
Direct (.tpl) Apply (.tpl)

Clamps a number within a specified range defined by min and max values.


Number Operations

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


Within range unchanged
INPUT
Value To Clamp: 5
Minimum Value: 0
Maximum Value: 10
OUTPUT
5
Below minimum clamped up
INPUT
Value To Clamp: -5
Minimum Value: 0
Maximum Value: 10
OUTPUT
0
Above maximum clamped down
INPUT
Value To Clamp: 15
Minimum Value: 0
Maximum Value: 10
OUTPUT
10

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

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

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


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