slugify โ GTM Variable Template for String
slugify EXTENDED String
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
When to Use This
Section titled โWhen to Use Thisโ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.
Examples
Section titled โExamplesโ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
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.
slugify
Text To Slugify
๐พ The string to convert into a URL-friendly slug.
Supported formats:
โ String
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
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
slugify()
Related Variables
Section titled โRelated VariablesโSame category: String
Under the Hood
Section titled โUnder the Hoodโ๐ 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