slice — GTM Variable Template for String
slice EXTENDED String
Extract and return a portion of a string or array starting from the provided starting index and optional ending index.
Examples
Extract substring
INPUT
Data To Slice: Hello World
Start Index: 0
End Index: 5
Start Index: 0
End Index: 5
OUTPUT
Hello
Negative index slices from end
INPUT
Data To Slice: abcdef
Start Index: -3
End Index: undefined
Start Index: -3
End Index: undefined
OUTPUT
def
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
slice
Data To Slice
💾 The string or array to slice.
Supported formats:
✓ String
✓ Array
Supported formats:
✓ String
✓ Array
Start Index
▶️ The starting index where to begin the slice.
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ String
End Index
⏹️ The index where to end the slice (optional, defaults to the end of the string/array).
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.
Data To Slice array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Start Index number
End Index number
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
slice()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Extract and return a portion of a string or array starting from the provided starting index and optional ending index.
*
* @param {string|Array} data.src - The string or array to slice.
* @param {number|string} data.sta - The starting index where to begin the slice.
* @param {number|string} data.end - The index where to end the slice (optional, defaults to the end of the string/array).
* @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 slicing.
*
* @returns {any} Returns a string or array containing the sliced portion, or undefined if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');
const slice = function(searchData, start, end) {
const startNum = makeNumber(start);
if (searchData == null || startNum !== startNum) { return undefined; }
if (!(getType(searchData) === 'array' || getType(searchData) === 'string')) {
return undefined;
}
const endNum = end === undefined ? searchData.length : makeNumber(end);
return searchData.slice(startNum, endNum);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// slice - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(slice(value, data.sta, data.end));
// ===============================================================================
// slice(...) – Apply Mode
// ===============================================================================
/*
return function(value, start, end) {
start = data.rp1 ? data.sta : start;
end = data.rp2 ? data.end : end;
return out(slice(value, start, end));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Extract substring'
✅ Array slice with start and end - returns subarray
✅ String slice with only start - slices to end
✅ '[example] Negative index slices from end'
✅ Invalid input - returns undefined