split — GTM Variable Template for String
split CORE String
Splits a string into an array of substrings based on a delimiter, with an optional limit on the number of substrings.
When to Use This
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Extraction
Pull specific values, segments, or patterns from complex data structures.
Examples
Split by comma
INPUT
String To Split: apple,banana,cherry
Delimiter: ,
Limit: undefined
Delimiter: ,
Limit: undefined
OUTPUT
['apple', 'banana', 'cherry']
Split with limit
INPUT
String To Split: hello world from paris
Delimiter:
Limit: 2
Delimiter:
Limit: 2
OUTPUT
['hello', 'world']
Non-string input returns empty array
INPUT
String To Split: 12345
Delimiter: ,
Limit: undefined
Delimiter: ,
Limit: undefined
OUTPUT
[]
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
split
String To Split
💾 The string to split into substrings.
Supported formats:
✓ String
Supported formats:
✓ String
Delimiter
✂️ The delimiter used to split the string.
Supported formats:
✓ String
Supported formats:
✓ String
Limit
🎯 The maximum number of substrings to return (optional).
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ 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 Split string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Delimiter string
Limit number
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
split()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Splits a string into substrings based on a delimiter, with an optional limit on the number of substrings.
*
* @param {string} data.src - The string to split.
* @param {string} data.del - The delimiter used to split the string.
* @param {number|string} data.lim - The maximum number of substrings to return (optional).
* @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 splitting.
*
* @returns {Array} Returns an array of substrings, or an empty array if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const split = function(input, delimiter, limit) {
if (typeof input !== 'string') return [];
if (limit !== undefined) {
const limitNum = makeNumber(limit);
if (limitNum !== limitNum || limitNum < 0) return []; // NaN check and negative check
return input.split(delimiter, limitNum);
}
return input.split(delimiter);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// split - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(split(value, data.del, data.lim));
// ===============================================================================
// split(...) – Apply Mode
// ===============================================================================
/*
return function(value, delimiter, limit) {
delimiter = data.rp1 ? data.del : delimiter;
limit = data.rp2 ? data.lim : limit;
return out(split(value, delimiter, limit));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Split by comma'
✅ '[example] Split with limit'
✅ String with no delimiter found - should return array with original string
✅ Empty string - should return array with empty string
✅ '[example] Non-string input returns empty array'