Skip to content

slugify โ€” GTM Variable Template for String

VARIABLES โ€บ STRING
slugify EXTENDED String
Direct (.tpl) Apply (.tpl)

Converts a string into a URL-friendly slug. Slugification steps: 1. Lowercase and trim 2. Replace accented characters with ASCII equivalents 3. Replace special characters with spaces 4. Collapse multiple spaces or hyphens into a single hyphen 5. Remove leading and trailing hyphens


String Manipulation

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

Formatting

Normalize casing, spacing, encoding, and presentation of data values.

URL Processing

Parse, build, decode, and manipulate URLs and query parameters.


Basic slugification
INPUT
Text To Slugify: Hello World! This is a Test
OUTPUT
hello-world-this-is-a-test
Accented characters
INPUT
Text To Slugify: Cafรฉ rรฉsumรฉ naรฏve
OUTPUT
cafe-resume-naive
Non-string returns undefined
INPUT
Text To Slugify: 12345
OUTPUT
undefined

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

slugify
Text To Slugify
๐Ÿ’พ The string to convert into a URL-friendly slug.

Supported formats:
  โœ“ String
Input Setup
Input Function (optional)
โš™๏ธ Optional pre-processing function applied to the input before slugification.
Result Handling
Output Function (optional)
โš™๏ธ Optional function to apply to the result before returning it (e.g., prepend a path prefix). Useful for chaining transformations on the output.
Text To Slugify string
๐Ÿ’ก Type any text to see the result update live
๐ŸŽฏ Using special value โ€” click input to type instead
Test with:
Falsy
Truthy
slugify()


๐Ÿ“œ View Implementation Code
/**
* Converts a string into a URL-friendly slug.
*
* Steps:
* 1. Lowercase and trim
* 2. Replace accented characters with ASCII equivalents
* 3. Replace non-alphanumeric characters with spaces
* 4. Collapse multiple spaces/hyphens into a single hyphen
* 5. Strip leading and trailing hyphens
*
* Uses the established replaceAllWithRegex pattern for GTM sandbox compatibility
* (no regex literals, no .normalize(), no .replaceAll()).
*
* @param {string} data.src - The string to slugify.
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function.
*
* @returns {string|undefined} The slugified string, or undefined if input is not a string.
*
* @framework ggLowCodeGTMKit
*/
const slugify = function(input) {
if (typeof input !== 'string') { return undefined; }
// --- Helper: replaceAllWithRegex (GTM sandbox pattern from toKebabCase/toSnakeCase) ---
const replaceAllWithRegex = function(str, pattern, replacement) {
if (typeof str !== 'string' || typeof pattern !== 'string') { return str; }
var result = str;
var lastIndex = 0;
while (lastIndex < result.length) {
var remaining = result.substring(lastIndex);
var matchObj = remaining.match(pattern);
if (matchObj === null) { break; }
var actualMatchIndex = lastIndex + (matchObj.index || 0);
result = result.substring(0, actualMatchIndex) +
replacement +
result.substring(actualMatchIndex + matchObj[0].length);
lastIndex = actualMatchIndex + replacement.length;
}
return result;
};
var slug = input.toLowerCase().trim();
var accentMap = {
'ร ': 'a', 'รก': 'a', 'รข': 'a', 'รฃ': 'a', 'รค': 'a', 'รฅ': 'a', 'รฆ': 'ae',
'รง': 'c', 'รจ': 'e', 'รฉ': 'e', 'รช': 'e', 'รซ': 'e',
'รฌ': 'i', 'รญ': 'i', 'รฎ': 'i', 'รฏ': 'i',
'รฐ': 'd', 'รฑ': 'n',
'รฒ': 'o', 'รณ': 'o', 'รด': 'o', 'รต': 'o', 'รถ': 'o', 'รธ': 'o',
'รน': 'u', 'รบ': 'u', 'รป': 'u', 'รผ': 'u',
'รฝ': 'y', 'รฟ': 'y', 'รพ': 'th',
'รŸ': 'ss', 'ล“': 'oe',
'ล‚': 'l', 'ฤ‘': 'd', 'ลพ': 'z', 'ลก': 's', 'ฤ': 'c', 'ล™': 'r',
'ฤ›': 'e', 'ลฏ': 'u', 'ล„': 'n', 'ล›': 's', 'ลบ': 'z', 'ฤ…': 'a', 'ฤ™': 'e',
'ฤ‡': 'c', 'ล‘': 'o', 'ลฑ': 'u'
};
var deaccented = '';
for (var i = 0; i < slug.length; i++) {
var ch = slug.charAt(i);
deaccented += accentMap[ch] || ch;
}
slug = deaccented;
slug = replaceAllWithRegex(slug, "[^a-z0-9\\s-]", ' ');
slug = slug.trim();
slug = replaceAllWithRegex(slug, "[\\s-]+", '-');
while (slug.length > 0 && slug.charAt(0) === '-') {
slug = slug.substring(1);
}
while (slug.length > 0 && slug.charAt(slug.length - 1) === '-') {
slug = slug.substring(0, slug.length - 1);
}
return slug;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.ou
๐Ÿงช View Test Scenarios (11 tests)
โœ… '[example] Basic slugification'
โœ… '[example] Accented characters'
โœ… German umlauts and eszett
โœ… Multiple spaces and special characters
โœ… Already a valid slug - unchanged
โœ… Numbers preserved
โœ… Mixed underscores and hyphens
โœ… French text with multiple accents
โœ… '[example] Non-string returns undefined'
โœ… Empty string returns empty string
โœ… Ampersands and special chars