startsWith — GTM Variable Template for String
startsWith EXTENDED String
Checks if the given string starts with the specified substring. Returns true if match found, false otherwise.
Examples
String starts with match
INPUT
String To Check: hello world
Search Term: hello
Search Term: hello
OUTPUT
true
No match at start
INPUT
String To Check: hello world
Search Term: world
Search Term: world
OUTPUT
false
Null input returns false
INPUT
String To Check: null
Search Term: test
Search Term: test
OUTPUT
false
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
startsWith
String To Check
💾 The string or value to check.
Supported formats:
✓ String
✓ Number
✓ Object
Supported formats:
✓ String
✓ Number
✓ Object
Search Term
💾 The substring to check if it is at the start of 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 Check number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Search Term string
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
startsWith()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Checks if the given string starts with the specified substring.
*
* @param {any} data.src - The string or value to check.
* @param {string} data.str - The substring to check if it is at the start of src.
* @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 checking.
*
* @returns {boolean} Returns true if the src starts with the str, otherwise returns false.
*
* @framework ggLowCodeGTMKit
*/
const startsWith = function(searchData, searchTerm) {
if (searchData == null || typeof searchTerm !== 'string') {
return false;
}
const searchDataString = searchData.toString();
return searchDataString.substring(0, searchTerm.length) === searchTerm;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// startsWith - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(startsWith(value, data.str));
// ===============================================================================
// startsWith(...) – Apply Mode
// ===============================================================================
/*
return function(value, searchTerm) {
searchTerm = data.rp1 ? data.str : searchTerm;
return out(startsWith(value, searchTerm));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] String starts with match'
✅ '[example] No match at start'
✅ Empty string with empty term - should return true
✅ Number starting with specified digits - should return true
✅ '[example] Null input returns false'