Skip to content

min β€” GTM Variable Template for Number

VARIABLES β€Ί NUMBER
min EXTENDED Number
Direct (.tpl) Apply (.tpl)

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



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

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


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