left β GTM Variable Template for String
left CORE String
Returns a substring from the start of a specified string, extracting the specified number of characters from the left.
When to Use This
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Extraction
Pull specific values, segments, or patterns from complex data structures.
Examples
Section titled βExamplesβExtract first 5 characters
INPUT
Source String: Hello World
Number of Characters: 5
Number of Characters: 5
OUTPUT
Hello
Count exceeds string length
INPUT
Source String: Hello
Number of Characters: 10
Number of Characters: 10
OUTPUT
Hello
Non-string input
INPUT
Source String: 12345
Number of Characters: 3
Number of Characters: 3
OUTPUT
""
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.
left
Source String
Returns a substring from the start of a specified string, extracting the specified number of characters from the left.
*** Extract first 5 characters***
Input: Source String: Hello World
Number of Characters: 5
βͺοΈ Output: Hello
*** Count exceeds string length***
Input: Source String: Hello
Number of Characters: 10
βͺοΈ Output: Hello
*** Non-string input***
Input: Source String: 12345
Number of Characters: 3
*** Extract first 5 characters***
Input: Source String: Hello World
Number of Characters: 5
βͺοΈ Output: Hello
*** Count exceeds string length***
Input: Source String: Hello
Number of Characters: 10
βͺοΈ Output: Hello
*** Non-string input***
Input: Source String: 12345
Number of Characters: 3
Number of Characters
π― The number of characters to extract from the left side of the string (default is 1).
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., normalize case, trim whitespace). Internal transformations such as string validation 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 + ' extracted' for string modifications). Useful for chaining transformations on the output.
Source String string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Number of Characters number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
left()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Returns a substring from the start of a specified string. * * @param {string} data.src - The string from which to extract the left portion. * @param {number|string} data.num - The number of characters to extract from the left side of the string (default is 1). * @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 extraction. * * @returns {string} The left portion of the string, or an empty string if num is 0. * * @framework ggLowCodeGTMKit */const makeNumber = require('makeNumber');const left = function(searchData, numCharacters) { const numChars = numCharacters !== undefined ? makeNumber(numCharacters) : 1; if (typeof searchData !== 'string' || typeof numChars !== 'number' || numChars <= 0) { return ''; } return searchData.slice(0, numChars);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// left - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(left(value, data.num));// ===============================================================================// left(...) β Apply Mode// ===============================================================================/*return function(value, numCharacters) { numCharacters = data.rp1 ? numCharacters : data.num; return out(left(value, numCharacters));};*/π§ͺ View Test Scenarios (10 tests)
β
'[example] Extract first 5 characters'β
Default behavior (1 character when num is undefined)β
Extract 0 characters (returns empty string)β
'[example] Count exceeds string length'β
String number parameterβ
Empty string inputβ
'[example] Non-string input'β
Negative number (returns empty string)β
UI-bound mode with static numβ
Single character extraction