Skip to content

parseIntValue — GTM Variable Template for Number

VARIABLES › NUMBER
parseIntValue CORE Number

Attempts to convert a given value into an integer. It extracts the numeric part from the start of the string and stops when it encounters a non-numeric character.


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.

Extraction

Pull specific values, segments, or patterns from complex data structures.


Examples

Parse integer string
INPUT
Value To Convert: 42
OUTPUT
42
Decimal truncated to integer
INPUT
Value To Convert: 3.14
OUTPUT
3
Non-numeric returns NaN
INPUT
Value To Convert: abc123
OUTPUT
true

GTM Configuration

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

parseIntValue
Value To Convert
Converts a value to an integer. Extracts the numeric part from the start of a string.

*** Parse integer string***
Input: 42
↪️ Output: 42

*** Decimal truncated to integer***
Input: 3.14
↪️ Output: 3

*** Non-numeric returns NaN***
Input: abc123
↪️ Output: true
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 number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
parseIntValue()


Under the Hood

📜 View Implementation Code
/**
 * Attempts to convert a given value into an integer.
 * It extracts the numeric part from the start of the string and stops when it encounters a non-numeric character.
 *
 * @param {any} data.src - The value to be converted to 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 parsing.
 * 
 * @returns {number} The parsed integer, or NaN if the value cannot be converted.
 *
 * @framework ggLowCodeGTMKit
 */

const makeInteger = require('makeInteger');
const getType = require('getType');

const parseIntValue = function(input) {
    const safeNaN = 0/0;
    if (input == null) { 
        return safeNaN; 
    }
    const inputString = input.toString();
    if (getType(inputString.match("^(\\d+)")) !== "null") {
        return makeInteger(inputString.match("^(\\d+)")[0]);
    }
    return safeNaN;
};

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

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

// ===============================================================================
// parseIntValue() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(parseIntValue(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Parse integer string'
✅ String starting with number - should extract leading digits
✅ '[example] Decimal truncated to integer'
✅ '[example] Non-numeric returns NaN'
✅ Null input - should return NaN