Skip to content

classifyByThreshold β€” GTM Variable Template for Number

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

Classifies a numeric value into categories based on threshold ranges.


Number Operations

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

Extraction

Pull specific values, segments, or patterns from complex data structures.

Comparison

Test equality, containment, and ordering between values.

Date & Time

Calculate durations, differences, and time-based operations on date values.


Classify into middle range
INPUT
Numeric Value: 75
Threshold Ranges: [
{min: 0, max: 60, cat: "F"},
{min: 60, max: 70, cat: "D"},
{min: 70, max: 80, cat: "C"},
{min: 80, max: 90, cat: "B"},
{min: 90, max: 100, cat: "A"}
]
OUTPUT
C
No match returns default
INPUT
Numeric Value: 150
Threshold Ranges: [
{min: 0, max: 50, cat: "Low"},
{min: 50, max: 100, cat: "High"}
]
No Match Handler (optional):
Out of Range
OUTPUT
Out of Range

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

classifyByThreshold
Numeric Value
πŸ’Ύ The numeric value to classify into a threshold category.

Supported formats:
  βœ“ Number: 75, 1250.5
  βœ“ Numeric string: "85"
Threshold Ranges
πŸ“‹ Define threshold ranges with their corresponding categories. First matching range wins.

Range logic: value >= min AND value < max

πŸ’‘ Tips:
  β€’ Leave Max empty for open-ended ranges (e.g., "100+")
  β€’ Order matters - first match is returned
  β€’ Use 'min' for range start, or use 'thr' for simple thresholds

Example:
  β€’ Min: 0, Max: 60, Category: F
  β€’ Min: 60, Max: 80, Category: C
  β€’ Min: 90, Max: (empty), Category: A

*** Classify into middle range***
Input: 75
β†ͺ️ Output: C

*** No match returns default***
Input: Numeric Value: 150
No Match Handler: Out of Range

β†ͺ️ Output: Out of Range
Min Value (or Threshold)Max Value (optional)Category
βŠ–
βŠ–
No Match Handler (optional)
πŸ’Ύ Value to return or function to call when no threshold matches or input is invalid.

Supported formats:
  βœ“ Default value: "Unknown", "N/A"
  βœ“ Callback function: val => "Out of range: " + val
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the numeric value before classification (e.g., round to integer, convert units).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the category result before returning it (e.g., cat => cat.toUpperCase(), cat => "Grade: " + cat). Useful for chaining transformations on the output.
Numeric Value number
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Threshold Ranges table
Min Value (or Threshold)Max Value (optional)Category
No Match Handler (optional) string
classifyByThreshold()


πŸ“œ View Implementation Code
/**
* Classifies a numeric value into categories based on threshold ranges.
* Returns the category for the first matching threshold.
*
* @param {number} data.src - The numeric value to classify.
* @param {Array} data.tbl - Array of objects with 'min' (or 'thr'), 'max' (optional), and 'cat' properties defining threshold ranges.
* @param {*|Function} [data.def] - Optional default value or callback function when no threshold matches.
* @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 classification.
*
* @returns {string|any} The category name for the matching threshold, or the default value if no match.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const classifyByThreshold = function(value, thresholds, defaultOrCallback) {
const numValue = makeNumber(value);
if (numValue !== numValue) {
if (typeof defaultOrCallback === 'function') {
return defaultOrCallback(value);
}
return defaultOrCallback;
}
for (let i = 0; i < thresholds.length; i++) {
const range = thresholds[i];
const minValue = range.min !== undefined ? makeNumber(range.min) : makeNumber(range.thr);
let maxValue = range.max !== undefined ? makeNumber(range.max) : undefined;
if (maxValue === undefined && i < thresholds.length - 1) {
const nextRange = thresholds[i + 1];
maxValue = nextRange.min !== undefined ? makeNumber(nextRange.min) : makeNumber(nextRange.thr);
}
const meetsMin = minValue !== minValue || numValue >= minValue;
const meetsMax = maxValue === undefined || maxValue !== maxValue || numValue < maxValue;
if (meetsMin && meetsMax) {
return range.cat;
}
}
if (typeof defaultOrCallback === 'function') {
return defaultOrCallback(numValue);
}
return defaultOrCallback;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// classifyByThreshold - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(classifyByThreshold(value, data.tbl, data.def));
// ===============================================================================
// classifyByThreshold(...) – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(classifyByThreshold(value, data.tbl, data.def));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Classify into middle range'
βœ… Classify value in first range
βœ… Classify value in open-ended last range using thr format
βœ… '[example] No match returns default'
βœ… Invalid numeric input returns default