Skip to content

padStart β€” GTM Variable Template for String

VARIABLES β€Ί STRING
padStart CORE String
Direct (.tpl) Apply (.tpl)

Pad a string or number from the left with a specific character to ensure it reaches a defined total length, useful for formatting values.


String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.

Formatting

Normalize casing, spacing, encoding, and presentation of data values.


Pad number with leading zeros
INPUT
String To Pad: 123
Target Length: 5
Pad Character: 0
OUTPUT
00123
String already exceeds target length
INPUT
String To Pad: hello world
Target Length: 5
Pad Character: 0
OUTPUT
hello world
Pad with custom character
INPUT
String To Pad: 42
Target Length: 6
Pad Character: *
OUTPUT
****42

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

padStart
String To Pad
πŸ’Ύ The input string or number to pad.

Supported formats:
  βœ“ String
  βœ“ Number
Target Length
πŸ’Ύ The final desired length of the output string.

Supported formats:
  βœ“ String
  βœ“ Number
Pad Character
πŸ’Ύ The character to use for padding (default is "0").

Supported formats:
  βœ“ String
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., convert number to string, normalize format). Internal transformations such as string conversion will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), val => val + ' formatted' for string modifications). Useful for chaining transformations on the output.
String To Pad number
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Target Length number
Pad Character number
padStart()


πŸ“œ View Implementation Code
/**
* Pads the start of a string with a given character until it reaches the desired length.
*
* @param {string|number} data.src - The input string or number to pad.
* @param {number} data.len - The final desired length of the output string.
* @param {string} data.chr - The character to use for padding (default is "0").
* @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 padding.
*
* @returns {string} The padded string.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const padStart = function(input, targetLength, padChar) {
if (input === null || input === undefined) return '';
const pad = (padChar !== undefined && padChar !== null && padChar !== '') ? padChar : '0';
const inputStr = input.toString();
const len = makeNumber(targetLength);
if (typeof len !== 'number' || len < 0) return inputStr;
const padNeeded = len - inputStr.length;
if (padNeeded <= 0) return inputStr;
let padding = '';
for (let i = 0; i < padNeeded; i++) {
padding += pad;
}
return padding + inputStr;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// padStart - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(padStart(value, data.len, data.chr));
// ===============================================================================
// padStart(...) – Apply Mode
// ===============================================================================
/*
return function(value, targetLength, padChar) {
targetLength = data.rp1 ? targetLength : data.len;
padChar = data.rp2 ? padChar : data.chr;
return out(padStart(value, targetLength, padChar));
};
*/
πŸ§ͺ View Test Scenarios (8 tests)
βœ… '[example] Pad number with leading zeros'
βœ… Pad string with spaces to length 10 - should pad with spaces
βœ… '[example] String already exceeds target length'
βœ… '[example] Pad with custom character'
βœ… Empty string with padding - should create padding only
βœ… Object input - should convert to string then pad
βœ… Undefined input - should return empty stringUntitled test 8
βœ… Null input - should return empty string