Skip to content

toCamelCase β€” GTM Variable Template for String

VARIABLES β€Ί STRING
toCamelCase CORE String
Direct (.tpl) Apply (.tpl)

Converts a string to camelCase format. Returns undefined if the input is not a valid string.


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.


Hyphens to camelCase
INPUT
String To Convert: hello-world-test
OUTPUT
helloWorldTest
Underscores to camelCase
INPUT
String To Convert: hello_world_test
OUTPUT
helloWorldTest
Non-string input returns undefined
INPUT
String To Convert: 12345
OUTPUT
undefined

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

toCamelCase
String To Convert
πŸ’Ύ The string to convert to camel case format.

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 camel 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.charAt(0).toUpperCase() + val.slice(1) for Pascal case). 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
toCamelCase()


πŸ“œ View Implementation Code
/**
* Converts a string to camel case format.
*
* @param {any} data.src - The string to convert to camel 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 camel case format, or undefined if the input is not a valid string.
*
* @framework ggLowCodeGTMKit
*/
const toCamelCase = function(string) {
if (typeof string === 'string') {
const pattern = "[_-]+";
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
.split(' ')
.map((word, index) => {
if (index === 0) {
return word.toLowerCase();
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join('');
return result;
}
return undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toCamelCase - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(toCamelCase(value));
// ===============================================================================
// toCamelCase() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(toCamelCase(value));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… String with spaces - should convert to camelCase
βœ… '[example] Hyphens to camelCase'
βœ… '[example] Underscores to camelCase'
βœ… Mixed case string with multiple separators - should convert to camelCase
βœ… '[example] Non-string input returns undefined'