Skip to content

takeItems — GTM Variable Template for Array

VARIABLES › ARRAY
takeItems EXTENDED Array

Returns the first N elements from an array.



Examples

Take first 3
INPUT
Array To Process: [1, 2, 3, 4, 5, 6]
Count: 3
OUTPUT
[1, 2, 3]
Count exceeds length
INPUT
Array To Process: ['a', 'b', 'c']
Count: 10
OUTPUT
['a', 'b', 'c']

GTM Configuration

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

takeItems
Array To Process
💾 The array to take items from.

Supported formats:
  ✓ Array
Count
💾 The number of items to take from the start.

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.
Array To Process array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Count number
takeItems()


Under the Hood

📜 View Implementation Code
/**
* Returns the first count elements from the array.
* 
* @param {Array} data.src - The array to take items from.
* @param {number|string} data.cnt - The number of items to take from the start.
* @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 taking items.
* 
* @returns {Array} A new array with up to count elements from the beginning, or empty array if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');

const takeItems = function(arr, count) {
   const cnt = makeNumber(count);
   if (getType(arr) !== 'array' || cnt !== cnt || cnt < 0) {
       return [];
   }
   return arr.slice(0, cnt);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// takeItems - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(takeItems(value, data.cnt));
// ===============================================================================
// takeItems(...) – Apply Mode
// ===============================================================================
/*
return function(value, count) {
   count = data.rp1 ? data.cnt : count;
   return out(takeItems(value, count));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Take first 3'
✅ '[example] Count exceeds length'
✅ String count parameter - converts to number and takes items
✅ Negative count - returns empty array
✅ Non-array input - returns empty array