Skip to content

min — GTM Variable Template for Number

VARIABLES › NUMBER
min EXTENDED Number

Returns the minimum (smallest) value from an array of numbers.



Examples

Minimum of positive numbers
INPUT
Numbers to Compare: [{arg: 10}, {arg: 25}, {arg: 15}, {arg: 5}]
OUTPUT
5
Minimum with mixed signs
INPUT
Numbers to Compare: [{arg: -10}, {arg: 0}, {arg: 15}, {arg: -3}, {arg: 8}]
OUTPUT
-10

GTM Configuration

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

min
Numbers to Compare
💾 A list of numbers to find the minimum value from. Non-numeric values will be ignored.

Supported formats:
  ✓ Number: 42, 3.14
  ✓ Variable reference

*** Minimum of positive numbers***

*** Minimum with mixed signs***
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the extracted numbers array before finding the minimum (e.g., filter values, transform array). Note: Values are extracted from the table's arg property before this function is applied.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the minimum value before returning it (e.g., val => val * 2, val => Math.abs(val)). Useful for chaining transformations on the output.
Numbers to Compare list
min()


Under the Hood

📜 View Implementation Code
/**
 * Returns the minimum value from an array of numbers.
 * 
 * @param {Array} data.src - Array of objects with 'arg' property (Direct mode) or array of numbers (Apply mode).
 * @param {Array} data.add - Additional numbers to include in the comparison (Apply mode only).
 * @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 processing.
 * 
 * @returns {number} The minimum value found, or NaN if the array is empty or contains no valid numbers.
 *
 * @framework ggLowCodeGTMKit
 */
const min = function(numberArray, additionalNumbers) {
    const additional = additionalNumbers || [];
    const combined = additional.length > 0 
        ? numberArray.concat(additional)
        : numberArray;
    const validNumbers = combined.filter(arg => typeof arg === 'number' && arg === arg);
    
    if (validNumbers.length === 0) return 0/0; // NaN
    
    let result = validNumbers[0];
    for (let i = 1; i < validNumbers.length; i++) {
        if (validNumbers[i] < result) {
            result = validNumbers[i];
        }
    }
    return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// min - Direct mode
// ===============================================================================
const extractArgumentValues = (items) => items.map(item => item ? item.arg : null);
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const extractedValues = extractArgumentValues(data.src);
const value = applyCast(data.pre, extractedValues);
return out(min(value));
// ===============================================================================
// min(...) – Apply Mode
// ===============================================================================
/*
return function(numberArray) {
   const extractAdditionalValues = (items) => items ? items.map(item => item ? item.arg : null) : [];
   const additionalNumbers = extractAdditionalValues(data.add);
   return out(min(numberArray, additionalNumbers));
};
*/
🧪 View Test Scenarios (8 tests)
✅ '[example] Minimum of positive numbers'
✅ Test finding minimum from negative numbers
✅ '[example] Minimum with mixed signs'
✅ Test with empty array returns NaN
✅ Test with single number
✅ Test finding minimum with additional numbers included
✅ Test with floor value from additional numbers
✅ Test with empty chained array but additional numbers provided