countDigits — GTM Variable Template for Number
countDigits EXTENDED Number
Returns the number of digits in the provided number.
Examples
Single digit returns 1
INPUT
Number To Count: 5
OUTPUT
1
Count digits in large number
INPUT
Number To Count: 123456
OUTPUT
6
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
countDigits
Number To Count
💾 A value that will be converted to a number for digit counting.
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.
Number To Count 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.
countDigits()
Related Variables
Same category: Number
Under the Hood
📜 View Implementation Code
/**
* Converts input data to a number and returns the number of digits.
*
* @param {number|string} data.src - A value that will be converted to a number.
* @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 counting.
*
* @returns {number} The count of digits in the converted number.
*
* @framework ggLowCodeGTMKit
*/
const Math = require('Math');
const makeNumber = require('makeNumber');
const countDigits = function(number) {
let num = makeNumber(number);
if (num !== num) { return undefined; } // NaN check
let count = 1;
while (num >= 10) {
num = Math.floor(num / 10);
count++;
}
return count;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// countDigits - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(countDigits(value));
// ===============================================================================
// countDigits() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(countDigits(value));
};
*/🧪 View Test Scenarios (6 tests)
✅ '[example] Single digit returns 1'
✅ Two digit number - returns 2
✅ '[example] Count digits in large number'
✅ String number - converts and counts digits
✅ Decimal number - counts digits before decimal point
✅ Invalid input - returns undefined