parseIntValue β GTM Variable Template for 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
Section titled β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
Section titled β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
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.
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
*** 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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
parseIntValue()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled β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