Skip to content

addValues β€” GTM Variable Template for Number

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

Adds two number values and returns the sum. Returns undefined if inputs are invalid.



Invalid input returns undefined
INPUT
First Number: 50
Second Number: undefined
OUTPUT
undefined

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

addValues
First Number
πŸ’Ύ The first number (the base value to add to).

Supported formats:
  βœ“ Number
  βœ“ String
Second Number
πŸ’Ύ The second number (the value to add).

Supported formats:
  βœ“ Number
  βœ“ String
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., convert string to number, apply transformations). Internal transformations such as number conversion will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., num => num.toFixed(2), val => val + ' total' for formatting). Useful for chaining transformations on the output.
First Number number
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Second Number number
addition()


πŸ“œ View Implementation Code
/**
* Adds two numbers and returns the result.
*
* @param {*} data.src - The first number (the base value to add to).
* @param {*} data.add - The second number (the value to add).
* @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 addition.
*
* @returns {number|undefined} The result of the addition, or undefined if invalid.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const addition = function(augend, addend) {
const num1 = makeNumber(augend);
const num2 = makeNumber(addend);
if (num1 === num1 && num2 === num2) {
return num1 + num2;
}
return undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// addition - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(addition(value, data.add));
// ===============================================================================
// addition(...) – Apply Mode
// ===============================================================================
/*
return function(value, addend) {
addend = data.rp1 ? addend : data.add;
return out(addition(value, addend));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… Valid numbers addition - returns sum
βœ… String numbers addition - converts and adds
βœ… Negative numbers addition - handles negative values
βœ… Invalid first parameter - returns undefined
βœ… '[example] Invalid input returns undefined'