Skip to content

slice β€” GTM Variable Template for String

VARIABLES β€Ί STRING
slice EXTENDED String
Direct (.tpl) Apply (.tpl)

Extract and return a portion of a string or array starting from the provided starting index and optional ending index.



Extract substring
INPUT
Data To Slice: Hello World
Start Index: 0
End Index: 5
OUTPUT
Hello
Negative index slices from end
INPUT
Data To Slice: abcdef
Start Index: -3
End Index: undefined
OUTPUT
def

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
Start Index
▢️ The starting index where to begin the slice.

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
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
slice()


πŸ“œ 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 ? start : data.sta;
end = data.rp2 ? end : data.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