Skip to content

toInteger — GTM Variable Template for Number

VARIABLES › NUMBER
toInteger CORE Number

Converts the input into an integer. Returns NaN if invalid.


When to Use This

Number Operations

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

Type Conversion

Safely convert between data types — strings, numbers, booleans, arrays, objects.


Examples

String to integer
INPUT
Value To Convert: 123
OUTPUT
123
Decimal rounded to integer
INPUT
Value To Convert: 45.89
OUTPUT
46
Invalid returns NaN
INPUT
Value To Convert: not a number
OUTPUT
""

GTM Configuration

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

toInteger
Value To Convert
💾 The value to be converted into an integer.

Supported formats:
  ✓ Any
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.
Value To Convert any
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
toInteger()


Under the Hood

📜 View Implementation Code
/**
* Converts the input into an integer.
* 
* @param {any} data.src - The value to be converted into an integer.
* @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 conversion.
* 
* @returns {number} The converted integer value, or NaN if the input cannot be converted.
*
* @framework ggLowCodeGTMKit
*/
const makeInteger = require('makeInteger');

const toInteger = function(input) {
  if (input === "0") return 0; 
   return makeInteger(input) || 0/0;
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// toInteger - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(toInteger(value));

// ===============================================================================
// toInteger() – Apply Mode
// ===============================================================================
/*
return function(value) {
  return out(toInteger(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ Valid integer - returns the integer
✅ '[example] String to integer'
✅ '[example] Decimal rounded to integer'
✅ Negative number - converts to negative integer
✅ '[example] Invalid returns NaN'