Skip to content

tail — GTM Variable Template for Array

VARIABLES › ARRAY
tail EXTENDED Array

Returns all elements of the array except the first one.



Examples

All except first
INPUT
Array To Extract From: [1, 2, 3, 4, 5]
OUTPUT
[2, 3, 4, 5]
Single element returns empty
INPUT
Array To Extract From: ['only']
OUTPUT
[]

GTM Configuration

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

tail
Array To Extract From
💾 The array to extract the tail from.

Supported formats:
  ✓ Array
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.
Array To Extract From array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
tail()


Under the Hood

📜 View Implementation Code
/**
 * Returns all elements of the array except the first one.
 * 
 * @param {Array} data.src - The array to extract the tail from.
 * @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 extracting tail.
 * 
 * @returns {Array} A new array containing all elements except the first, or an empty array if input is invalid or has fewer than 2 elements.
 *
 * @framework ggLowCodeGTMKit
 */
const getType = require('getType');

const tail = function(arr) {
    if (getType(arr) !== 'array' || arr.length < 2) {
        return [];
    }
    return arr.slice(1);
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// tail - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(tail(value));

// ===============================================================================
// tail() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(tail(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] All except first'
✅ Array with two elements - returns last element as array
✅ '[example] Single element returns empty'
✅ Empty array - returns empty array
✅ Non-array input - returns empty array