addValues β GTM Variable Template for Number
addValues EXTENDED Number
Adds two number values and returns the sum. Returns undefined if inputs are invalid.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβInvalid input returns undefined
INPUT
First Number: 50
Second Number: undefined
Second Number: undefined
OUTPUT
undefined
Live Sandbox
Section titled βLive Sandboxβ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
Supported formats:
β Number
β String
Second Number
πΎ The second number (the value to add).
Supported formats:
β Number
β String
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
addition()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ 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'