substringAfter β GTM Variable Template for String
substringAfter EXTENDED String
Returns the substring after the first occurrence of a delimiter. If the delimiter is not found, it returns an empty string.
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βGet email domain
INPUT
String To Process: [email protected]
Delimiter: @
Delimiter: @
OUTPUT
example.com
Delimiter not found
INPUT
String To Process: no-delimiter-here
Delimiter: @
Delimiter: @
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.
substringAfter
String To Process
πΎ The string to process.
Supported formats:
β String
Supported formats:
β String
Delimiter
βοΈ The delimiter to search for.
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 Process string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Delimiter string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
substringAfter()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Returns the substring after the first occurrence of a delimiter. * * @param {string} data.src - The string to process. * @param {string} data.del - The delimiter to search for. * @param {Function|string} [data.out] - Optional output handler. * * Direct-mode specific parameters: * @param {Function} [data.pre] - Optional pre-processor function to transform src. * * @returns {string|undefined} Returns the substring after the delimiter, an empty string if not found, or undefined if input is invalid. * * @framework ggLowCodeGTMKit */const substringAfter = function(input, delimiter) { if (typeof input !== 'string') return undefined; if (typeof delimiter !== 'string') return '';
const index = input.indexOf(delimiter); if (index === -1) { return ''; } return input.substring(index + delimiter.length);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// substringAfter - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(substringAfter(value, data.del));// ===============================================================================// substringAfter(...) β Apply Mode// ===============================================================================/*return function(value, delimiter) { delimiter = data.rp1 ? delimiter : data.del; return out(substringAfter(value, delimiter));};*/π§ͺ View Test Scenarios (4 tests)
β
'[example] Get email domain'β
'[example] Delimiter not found'β
Empty string returns empty stringβ
Non-string returns undefined