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
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Extraction
Pull specific values, segments, or patterns from complex data structures.
Examples
Split by comma
INPUT
String To Split: hello-world-test
Separator: -
Separator: -
OUTPUT
hello
Email username extraction
INPUT
String To Split: user@example.com
Separator: @
Separator: @
OUTPUT
user
Non-string input returns original
INPUT
String To Split: 12345
Separator: -
Separator: -
OUTPUT
12345
GTM Configuration
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: user@example.com
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: user@example.com
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
Same category: String
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 ? data.sep : separator;
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'