extractFirstSegment β GTM Variable Template for String
extractFirstSegment CORE String
Returns the first part of a string split by a given separator. If the input is not a valid string, the original value is returned.
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βSplit by comma
INPUT
String To Split: hello-world-test
Separator: -
Separator: -
OUTPUT
hello
Email username extraction
INPUT
String To Split: [email protected]
Separator: @
Separator: @
OUTPUT
user
Non-string input returns original
INPUT
String To Split: 12345
Separator: -
Separator: -
OUTPUT
12345
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.
extractFirstSegment
String To Split
Returns the first part of a string split by a given separator. If the input is not a valid string, the original value is returned.
*** Split by comma***
Input: String To Split: hello-world-test
Separator: -
βͺοΈ Output: hello
*** Email username extraction***
Input: String To Split: [email protected]
Separator: @
βͺοΈ Output: user
*** Non-string input returns original***
Input: String To Split: 12345
Separator: -
βͺοΈ Output: 12345
*** Split by comma***
Input: String To Split: hello-world-test
Separator: -
βͺοΈ Output: hello
*** Email username extraction***
Input: String To Split: [email protected]
Separator: @
βͺοΈ Output: user
*** Non-string input returns original***
Input: String To Split: 12345
Separator: -
βͺοΈ Output: 12345
Separator
πΎ The separator to use for splitting the string.
Supported formats:
β String
Supported formats:
β 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.
String To Split string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Separator string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
extractFirstSegment()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Returns the first part of a string split by a given separator. * If the input is not a valid string, the original value is returned. * * @param {string} data.src - The string to split. * @param {string} data.sep - The separator to use for splitting the string. * @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 splitting. * * @returns {string} The first segment of the split string, or the original value if input is not a string. * * @framework ggLowCodeGTMKit */const extractFirstSegment = function(inputString, separator) { if (typeof inputString !== 'string') { return inputString; } const parts = inputString.split(separator); return parts[0];};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// extractFirstSegment - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(extractFirstSegment(value, data.sep));// ===============================================================================// extractFirstSegment(...) β Apply Mode// ===============================================================================/*return function(value, separator) { separator = data.rp1 ? separator : data.sep; return out(extractFirstSegment(value, separator));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Split by comma'β
String without separator - should return original stringβ
'[example] Email username extraction'β
String starting with separator - should return empty stringβ
'[example] Non-string input returns original'