max — GTM Variable Template for Number
max CORE Number
Returns the maximum (largest) value from an array of numbers.
When to Use This
Number Operations
Arithmetic, rounding, clamping, and mathematical transformations on numeric values.
Filtering
Select or exclude items from collections based on criteria or predicates.
Examples
Maximum of positive numbers
INPUT
add: []
OUTPUT
25
Maximum with negatives
INPUT
add: [{arg: -1}]
Numbers to Compare: [{arg: -5}, {arg: -2}, {arg: -10}, {arg: -1}]
Numbers to Compare: [{arg: -5}, {arg: -2}, {arg: -10}, {arg: -1}]
OUTPUT
-1
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
max
Numbers to Compare
💾 A list of numbers to find the maximum value from. Non-numeric values will be ignored.
Supported formats:
✓ Number: 42, 3.14
✓ Variable reference
*** Maximum of positive numbers***
*** Maximum with negatives***
Supported formats:
✓ Number: 42, 3.14
✓ Variable reference
*** Maximum of positive numbers***
*** Maximum with negatives***
⊖
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the extracted numbers array before finding the maximum (e.g., filter values, transform array). Note: Values are extracted from the table's
argument property before this function is applied.Result Handling
Output Function (optional)
⚙️ Optional function to apply to the maximum value before returning it (e.g.,
val => val * 2, val => Math.round(val)). Useful for chaining transformations on the output.Numbers to Compare list
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
max()
Related Variables
Same category: Number
Under the Hood
📜 View Implementation Code
/**
* Returns the maximum 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 maximum value found, or NaN if the array is empty or contains no valid numbers.
*
* @framework ggLowCodeGTMKit
*/
const max = 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);
// ===============================================================================
// max - 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(max(value));
// ===============================================================================
// max(...) – Apply Mode
// ===============================================================================
/*
return function(numberArray) {
const extractAdditionalValues = (items) => items ? items.map(item => item ? item.arg : null) : [];
const additionalNumbers = extractAdditionalValues(data.add);
return out(max(numberArray, additionalNumbers));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Maximum of positive numbers'
✅ Test finding maximum with additional numbers (Apply Mode only feature)
✅ Test with ceiling value
✅ '[example] Maximum with negatives'
✅ Test with single number