chunk β GTM Variable Template for Array
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβSplit into equal chunks
INPUT
Array To Split: [1, 2, 3, 4, 5, 6]
Chunk Size: 2
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
Chunk Size: 3
OUTPUT
[[1, 2, 3], [4, 5, 6], [7]]
Live Sandbox
Section titled βLive Sandboxβ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
Supported formats:
β Array
Chunk Size
π― The size of each chunk (must be >= 1).
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.
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
chunk()
Related Variables
Section titled βRelated VariablesβSame category: Array
Under the Hood
Section titled β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 ? size : data.siz; 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