๐๐๐๐๐ From Indexed Values Generator - Array โ GTM Variable Template for GTM
๐๐๐๐๐ From Indexed Values Generator - Array CORE GTM
Generates an array from indexed table values, ordered by index.
When to Use This
GTM Utilities
Access GTM-specific APIs: dataLayer, debug mode, container settings.
GA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Sorting
Order arrays and collections by values, keys, or custom comparators.
Examples
Transform with mapper
INPUT
Indexed Values: [
Append unindexed values at the end: true
Mapper Function (optional): val => val.toUpperCase()
Append unindexed values at the end: true
Mapper Function (optional): val => val.toUpperCase()
OUTPUT
['HELLO', 'WORLD', 'EXTRA']
Array ordered by index
INPUT
Indexed Values: [
Append unindexed values at the end: true
Append unindexed values at the end: true
OUTPUT
['first', 'second', 'third']
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
๐๐๐๐๐ From Indexed Values Generator - Array
ARRAY โฌ
Indexed Values
๐พ Define values with optional indices.
Index column:
โข Valid indices: Non-negative numbers (0, 1, 2, ...)
โข Invalid/empty indices: Values are appended at the end (if enabled)
โข Gaps in indices: Filled with
Value column:
โข Any value (string, number, variable, etc.)
Index column:
โข Valid indices: Non-negative numbers (0, 1, 2, ...)
โข Invalid/empty indices: Values are appended at the end (if enabled)
โข Gaps in indices: Filled with
undefinedValue column:
โข Any value (string, number, variable, etc.)
IndexValue
โ
โ
โ When enabled, values without valid indices are appended after the indexed values. When disabled, unindexed values are excluded from the result.
Value Processing
Mapper Function (optional)
โ๏ธ Optional function to transform each individual value before adding it to the array:
Use this for:
โข Trim whitespace:
โข Parse numbers:
โข Transform case:
โข Add prefix:
Note: This applies to each value individually, including both indexed and unindexed values.
val => transformedValUse this for:
โข Trim whitespace:
val => val.trim()โข Parse numbers:
val => parseFloat(val)โข Transform case:
val => val.toUpperCase()โข Add prefix:
val => 'item_' + valNote: This applies to each value individually, including both indexed and unindexed values.
Result Handling
Output Function (optional)
โ๏ธ Optional function to transform the entire array before returning it:
Use this for:
โข Remove gaps:
โข Convert to string:
โข Reverse array:
Note: This applies to the complete array after all values have been processed and positioned.
arr => transformedArrUse this for:
โข Remove gaps:
arr => arr.filter(x => x !== undefined)โข Convert to string:
arr => arr.join(',')โข Reverse array:
arr => arr.reverse()Note: This applies to the complete array after all values have been processed and positioned.
Indexed Values table
IndexValue
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
getType()
Related Variables
Same category: GTM
Under the Hood
๐ View Implementation Code
/**
* Converts indexed objects into a properly ordered array with optional unindexed values appended.
*
* @param {Array<{ind?: number, val: any}>} data.ivl - Array of objects with optional index (ind) and value (val).
* @param {boolean} [data.aui=true] - Whether to append unindexed values at the end.
* @param {Function} [data.map] - Optional function to transform each value before adding to array.
* @param {Function|string} [data.out] - Optional output handler.
*
* @returns {Array} Ordered array with indexed values and optionally appended unindexed values.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');
const indexedObjects = getType(data.ivl) === 'array' && data.ivl || [];
const appendUnindexed = typeof data.aui !== 'undefined' ? data.aui : true;
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const mapper = safeFunction(data.map);
const indexed = [];
const unindexed = [];
indexedObjects.forEach(obj => {
if (obj && obj.hasOwnProperty('ind') && obj.ind !== null && obj.ind !== undefined) {
const idx = makeNumber(obj.ind);
if (typeof idx === 'number' && idx >= 0 && idx === idx) {
indexed.push(obj);
} else if (obj.hasOwnProperty('val')) {
unindexed.push(obj);
}
} else if (obj && obj.hasOwnProperty('val')) {
unindexed.push(obj);
}
});
indexed.sort((a, b) => makeNumber(a.ind) - makeNumber(b.ind));
let maxIndex = -1;
indexed.forEach(obj => {
const idx = makeNumber(obj.ind);
if (idx > maxIndex) {
maxIndex = idx;
}
});
const result = [];
result.length = maxIndex + 1;
indexed.forEach(obj => {
result[makeNumber(obj.ind)] = mapper(obj.val);
});
if (appendUnindexed) {
unindexed.forEach(obj => result.push(mapper(obj.val)));
}
const out = safeFunction(data.out);
return out(result);๐งช View Test Scenarios (11 tests)
โ
'[example] Transform with mapper'
โ
'[example] Array ordered by index'
โ
Array with gaps - should fill with undefined
โ
Unindexed values appended - should add at the end
โ
Unindexed values not appended - should exclude them
โ
Mixed indexed and unindexed with gaps
โ
Invalid indices - should treat as unindexed
โ
Empty array - should return empty array
โ
Duplicate indices - last one should win
โ
Output function to remove undefined - should filter gaps
โ
Unsorted indices - should sort properly