Skip to content

substringBefore β€” GTM Variable Template for String

VARIABLES β€Ί STRING
substringBefore EXTENDED String
Direct (.tpl) Apply (.tpl)

Returns the substring before the first occurrence of a delimiter. If the delimiter is not found, it returns the original string.


String Manipulation

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

Extraction

Pull specific values, segments, or patterns from complex data structures.


Get email username
INPUT
String To Process: [email protected]
Delimiter: @
OUTPUT
jane.doe
Delimiter not found
INPUT
String To Process: no-delimiter-here
Delimiter: @
OUTPUT
no-delimiter-here

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

substringBefore
String To Process
πŸ’Ύ The string to process.

Supported formats:
  βœ“ String
Delimiter
βœ‚οΈ The delimiter to search for.

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
substringBefore()


πŸ“œ View Implementation Code
/**
* Returns the substring before 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 before the delimiter, the original string if not found, or undefined if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const substringBefore = function(input, delimiter) {
if (typeof input !== 'string') return undefined;
if (typeof delimiter !== 'string') return input;
const index = input.indexOf(delimiter);
if (index === -1) {
return input;
}
return input.substring(0, index);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// substringBefore - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(substringBefore(value, data.del));
// ===============================================================================
// substringBefore(...) – Apply Mode
// ===============================================================================
/*
return function(value, delimiter) {
delimiter = data.rp1 ? delimiter : data.del;
return out(substringBefore(value, delimiter));
};
*/
πŸ§ͺ View Test Scenarios (4 tests)
βœ… '[example] Get email username'
βœ… '[example] Delimiter not found'
βœ… Empty string returns empty string
βœ… Non-string returns undefined