safeSplit — GTM Variable Template for String
safeSplit CORE String
Safely splits a string into an array using the specified separator. If input is already an array, returns it as-is. Returns empty array for invalid input.
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
Input To Split: apple,banana,cherry
Separator: ,
Separator: ,
OUTPUT
["apple", "banana", "cherry"]
Split by pipe
INPUT
Input To Split: one|two|three
Separator: |
Separator: |
OUTPUT
["one", "two", "three"]
Invalid input returns empty array
INPUT
Input To Split: 12345
Separator: ,
Separator: ,
OUTPUT
[]
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
safeSplit
Input To Split
💾 String to split or array to return as-is.
Supported formats:
✓ String
✓ Array
Supported formats:
✓ String
✓ Array
Separator
🔪 The separator character to split by (default: comma).
Supported formats:
✓ String
Supported formats:
✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., trim whitespace, normalize format). Internal transformations such as splitting will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., arr => arr.length for count, arr => arr.join('|') to rejoin). Useful for chaining transformations on the output.
Input To Split array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Separator string
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
safeSplit()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Safely splits a string into an array, returns array as-is if already array.
*
* @param {string|Array} data.src - String to split or array to return.
* @param {string} [data.sep] - Separator (default: ',').
* @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} Array of elements, or empty array if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const safeSplit = function(input, separator) {
if (getType(input) === 'array') return input;
if (typeof input !== 'string') return [];
const sep = separator || ',';
return input.split(sep);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// safeSplit - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(safeSplit(value, data.sep));
// ===============================================================================
// safeSplit(...) – Apply Mode
// ===============================================================================
/*
return function(value, separator) {
separator = data.rp1 ? data.sep : separator;
return out(safeSplit(value, separator));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Split by comma'
✅ '[example] Split by pipe'
✅ Array input - returns array as-is
✅ Empty string - returns array with one empty string
✅ '[example] Invalid input returns empty array'