divide β GTM Variable Template for Number
divide CORE Number
Divides the numerator by the denominator. Returns undefined for division by zero.
When to Use This
Section titled βWhen to Use ThisβNumber Operations
Arithmetic, rounding, clamping, and mathematical transformations on numeric values.
Examples
Section titled βExamplesβSimple division
INPUT
Numerator: 10
Denominator: 2
Denominator: 2
OUTPUT
5
Division by zero returns undefined
INPUT
Numerator: 10
Denominator: 0
Denominator: 0
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.
divide
Numerator
πΎ The number to divide (dividend).
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Denominator
πΎ The number to divide by (divisor).
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 object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Numerator number
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Denominator number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
divide()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Divides the numerator by the denominator and returns the result.** @param {*} data.src - The number to divide (dividend).* @param {*} data.div - The number to divide by (divisor).* @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 division.** @returns {number|undefined} The result of the division, or undefined if invalid.** @framework ggLowCodeGTMKit*/const makeNumber = require('makeNumber');
const divide = function(numerator, denominator) { const num = makeNumber(numerator); const den = makeNumber(denominator); if (num === num && den === den && den !== 0) { return num / den; } return undefined;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// divide - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(divide(value, data.div));// ===============================================================================// divide(...) β Apply Mode// ===============================================================================/*return function(value, denominator) { denominator = data.rp1 ? denominator : data.div; return out(divide(value, denominator));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Simple division'β
String numbers - converts and dividesβ
Decimal result - returns decimalβ
'[example] Division by zero returns undefined'β
Invalid input - returns undefined