Skip to content

classifyByThreshold — GTM Variable Template for Number

VARIABLES › NUMBER
classifyByThreshold CORE Number

Classifies a numeric value into categories based on threshold ranges.


When to Use This

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.


Examples

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

GTM Configuration

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


Under the Hood

📜 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