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
Section titled β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
Section titled β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
[]
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.
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
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled β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 ? delimiter : data.del; limit = data.rp2 ? limit : data.lim; 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'