toSnakeCase β GTM Variable Template for String
toSnakeCase CORE String
Converts a string to snake_case format. Returns undefined if the input is not a valid string.
When to Use This
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Formatting
Normalize casing, spacing, encoding, and presentation of data values.
Examples
Section titled βExamplesβSpaces to snake_case
INPUT
String To Convert: hello world test
OUTPUT
hello_world_test
Hyphens to snake_case
INPUT
String To Convert: hello-world-test
OUTPUT
hello_world_test
Non-string input returns undefined
INPUT
String To Convert: 12345
OUTPUT
undefined
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.
toSnakeCase
String To Convert
πΎ The string to convert to snake_case format.
Supported formats:
β String
Supported formats:
β String
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., normalize case, clean string). Internal transformations such as snake_case conversion will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + '_suffix', val => val.replace(/^_/, '') to remove leading underscores). Useful for chaining transformations on the output.
String To Convert string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
toSnakeCase()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Converts a string to snake_case format.** @param {string} data.src - The string to convert to snake_case.* @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 conversion.** @returns {string|undefined} The string in snake_case format, or undefined if the input is not a valid string.** @framework ggLowCodeGTMKit*/const toSnakeCase = function(string) { if (typeof string === 'string') { const pattern = "[_\\s-]+"; const replacement = '_';
const replaceAllWithRegex = function(input, pattern, replacement) { if (typeof input !== 'string' || typeof pattern !== 'string' || typeof replacement !== 'string') { return input; } let result = input; let lastIndex = 0;
while (lastIndex < result.length) { const remainingStr = result.substring(lastIndex); const matchObj = remainingStr.match(pattern); if (matchObj === null) { break; }
const actualMatchIndex = lastIndex + (matchObj.index || 0);
result = result.substring(0, actualMatchIndex) + replacement + result.substring(actualMatchIndex + matchObj[0].length); lastIndex = actualMatchIndex + replacement.length; } return result; };
let result = replaceAllWithRegex(string, pattern, replacement);
result = result.toLowerCase(); return result; } return undefined;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// toSnakeCase - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(toSnakeCase(value));// ===============================================================================// toSnakeCase() β Apply Mode// ===============================================================================/*return function(value) { return out(toSnakeCase(value));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Spaces to snake_case'β
'[example] Hyphens to snake_case'β
Mixed case string with multiple separators - should convert to snake_caseβ
Already in snake_case - should remain unchangedβ
'[example] Non-string input returns undefined'