Skip to content

⚡ ARRAY › From Values Generator — GTM Variable Template for Array

VARIABLES › ARRAY
⚡ ARRAY › From Values EXTENDED Array
Direct (.tpl)

Generates an array from a simple GTM table of values.


GTM Utilities

Access GTM-specific APIs: dataLayer, debug mode, container settings.

Type Conversion

Safely convert between data types — strings, numbers, booleans, arrays, objects.

Extraction

Pull specific values, segments, or patterns from complex data structures.


Array from table values
INPUT
Array Values: [{val: 'apple'}, {val: 'banana'}, {val: 'cherry'}]
OUTPUT
['apple', 'banana', 'cherry']
Empty table returns empty
INPUT
Array Values: []
OUTPUT
[]

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

⚡ ARRAY › From Values
ARRAY ⬇
Array Values
💾 Define the values to include in the array.

Each row will become an element in the resulting array. Empty values and variables are preserved.
Value Processing
Mapper Function (optional)
⚙️ Optional function to transform each individual value before adding it to the array: val => transformedVal

Use this for:
  • Split strings: val => val.split(',')
  • Trim whitespace: val => val.trim()
  • Parse numbers: val => parseFloat(val)
  • Transform case: val => val.toUpperCase()

Note: This applies to each value individually, before creating the final array.
Result Handling
Output Function (optional)
⚙️ Optional function to transform the entire array before returning it: arr => transformedArr

Use this for:
  • Filter array: arr => arr.filter(x => x)
  • Flatten nested arrays: arr => arr.flat()
  • Convert to string: arr => arr.join(',')
  • Sort array: arr => arr.sort()

Note: This applies to the complete array after all values have been processed.
Array Values list
buildArrayFromValues()


📜 View Implementation Code
/**
* Creates an array from a simple table by extracting and optionally transforming values from the 'val' column.
*
* @param {Array<Object>} data.tbl - The array of objects representing the simple table.
* @param {Function} [data.map] - Optional function to transform each value before adding to array.
* @param {Function|string} [data.out] - Optional output handler.
*
* @returns {Array} An array of values extracted from the 'val' column.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const buildArrayFromValues = function(tbl, map) {
const simpleTable = getType(tbl) === 'array' && tbl || [];
const mapper = typeof map === 'function' ? map : x => x;
const result = [];
for (let i = 0; i < simpleTable.length; i++) {
const item = simpleTable[i];
if (item) {
result.push(mapper(item.val));
}
}
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
return out(buildArrayFromValues(data.tbl, data.map));
🧪 View Test Scenarios (10 tests)
✅ '[example] Array from table values'
✅ '[example] Empty table returns empty'
✅ Table with undefined and empty values - should preserve them
✅ Table with null items - should skip null items
✅ Mapper function to transform each value - should apply to all values
✅ Mapper with split function - should split each string into array
✅ Output function to filter array - should remove empty values
✅ Output function to join array - should convert to CSV string
✅ Combination of mapper and output - should apply mapper first then output
✅ Different value types - should preserve types