Skip to content

๐€๐‘๐‘๐€๐˜ From Values Generator - Array โ€” GTM Variable Template for GTM

VARIABLES โ€บ GTM
๐€๐‘๐‘๐€๐˜ From Values Generator - Array CORE GTM

Generates an array from a simple GTM table of values.


When to Use This

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.


Examples

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

GTM Configuration

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

๐€๐‘๐‘๐€๐˜ From Values Generator - Array
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
getType()


Under the Hood

๐Ÿ“œ 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 simpleTable = getType(data.tbl) === 'array' && data.tbl || [];
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const mapper = safeFunction(data.map);

const result = [];

for (let i = 0; i < simpleTable.length; i++) {
    const item = simpleTable[i];
    if (item) {
        result.push(mapper(item.val));
    }
}

const out = safeFunction(data.out);

return out(result);
๐Ÿงช 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