Skip to content

chunk — GTM Variable Template for Array

VARIABLES › ARRAY
chunk EXTENDED Array

Splits an array into groups of the specified size.



Examples

Split into equal chunks
INPUT
Array To Split: [1, 2, 3, 4, 5, 6]
Chunk Size: 2
OUTPUT
[[1, 2], [3, 4], [5, 6]]
Remainder in last chunk
INPUT
Array To Split: [1, 2, 3, 4, 5, 6, 7]
Chunk Size: 3
OUTPUT
[[1, 2, 3], [4, 5, 6], [7]]

GTM Configuration

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

chunk
Array To Split
💾 The array to split into chunks.

Supported formats:
  ✓ Array
Chunk Size
🎯 The size of each chunk (must be >= 1).

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 Split array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Chunk Size number
chunk()


Under the Hood

📜 View Implementation Code
/**
* Splits an array into chunks of the specified size.
* 
* @param {Array} data.src - The array to split into chunks.
* @param {number|string} data.siz - The size of each chunk (must be >= 1).
* @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 chunking.
* 
* @returns {Array[]} A new array containing sub-arrays (chunks) of the specified size. The last chunk may be smaller if the array can't be split evenly.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');

const chunk = function(arr, size) {
   const sizeNum = makeNumber(size);
   if (getType(arr) !== 'array' || typeof sizeNum !== 'number' || sizeNum < 1) {
       return [];
   }
   const result = [];
   for (let i = 0; i < arr.length; i += sizeNum) {
       result.push(arr.slice(i, i + sizeNum));
   }
   return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// chunk - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(chunk(value, data.siz));
// ===============================================================================
// chunk(...) – Apply Mode
// ===============================================================================
/*
return function(value, size) {
   size = data.rp1 ? data.siz : size;
   return out(chunk(value, size));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Split into equal chunks'
✅ '[example] Remainder in last chunk'
✅ String size parameter - converts to number and chunks
✅ Invalid size less than 1 - returns empty array
✅ Non-array input - returns empty array