Skip to content

startsWith β€” GTM Variable Template for String

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

Checks if the given string starts with the specified substring. Returns true if match found, false otherwise.



String starts with match
INPUT
String To Check: hello world
Search Term: hello
OUTPUT
true
No match at start
INPUT
String To Check: hello world
Search Term: world
OUTPUT
false
Null input returns false
INPUT
String To Check: null
Search Term: test
OUTPUT
false

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
Search Term
πŸ’Ύ The substring to check if it is at the start of the 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
startsWith()


πŸ“œ 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 ? searchTerm : data.str;
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'