Skip to content

divide — GTM Variable Template for Number

VARIABLES › NUMBER
divide CORE Number

Divides the numerator by the denominator. Returns undefined for division by zero.


When to Use This

Number Operations

Arithmetic, rounding, clamping, and mathematical transformations on numeric values.


Examples

Simple division
INPUT
Numerator: 10
Denominator: 2
OUTPUT
5
Division by zero returns undefined
INPUT
Numerator: 10
Denominator: 0
OUTPUT
undefined

GTM Configuration

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
Denominator
💾 The number to divide by (divisor).

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


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 ? data.div : denominator;
   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