itemSet — GTM Variable Template for Items
itemSet CORE Items
Adds or overwrites a property on each object in an array using a callback or static value.
When to Use This
GA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Examples
Add computed property
INPUT
Source Array: [
{price: 10, quantity: 2},
{price: 20, quantity: 3}
]
Value Function: function(item) { return item.price * item.quantity; }
Property Key: total
Value Mode: fn
{price: 10, quantity: 2},
{price: 20, quantity: 3}
]
Value Function: function(item) { return item.price * item.quantity; }
Property Key: total
Value Mode: fn
OUTPUT
[ {price: 10, quantity: 2, total: 20}, {price: 20, quantity: 3, total: 60} ]
Set static value
INPUT
Source Array: [
{price: 10, quantity: 2},
{price: 20, quantity: 3}
]
Property Key: currency
Value Mode: val
Static Value: EUR
{price: 10, quantity: 2},
{price: 20, quantity: 3}
]
Property Key: currency
Value Mode: val
Static Value: EUR
OUTPUT
[ {price: 10, quantity: 2, currency: 'EUR'}, {price: 20, quantity: 3, currency: 'EUR'} ]
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
itemSet
Source Array
💾 The array of objects to process.
Supported formats:
✓ Items variable: {{ecommerce.items}}
✓ Array literal: [{price: 10, quantity: 2}]
Supported formats:
✓ Items variable: {{ecommerce.items}}
✓ Array literal: [{price: 10, quantity: 2}]
Property Key
💾 The property name to add or overwrite on each item.
Supported formats:
✓ New property: "total", "margin", "currency"
✓ Existing property: "price" (will be overwritten)
Supported formats:
✓ New property: "total", "margin", "currency"
✓ Existing property: "price" (will be overwritten)
Value Mode
💾 How to determine the value to set on each item.
✓ Computed: uses a callback function receiving the item
✓ Static: sets the same value on every item
✓ Computed: uses a callback function receiving the item
✓ Static: sets the same value on every item
Value Function
💾 A function that receives each item and returns the value to set.
Supported formats:
✓ applyFromObject: {{applyFromObject(...)}}
✓ Custom function: item => Math.round(item.price)
Supported formats:
✓ applyFromObject: {{applyFromObject(...)}}
✓ Custom function: item => Math.round(item.price)
Static Value
💾 The static value to set on every item.
Supported formats:
✓ String: "EUR", "active"
✓ Number: 1, 0
✓ Variable: {{currency}}
Supported formats:
✓ String: "EUR", "active"
✓ Number: 1, 0
✓ Variable: {{currency}}
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the source array before processing.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the resulting array before returning it (e.g.,
arr => arr.filter(x => x.total > 0) to keep only items with a positive total).Source Array array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Property Key string
Value Function function
Static Value number
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemSetProperty()
Related Variables
Same category: Items
Under the Hood
📜 View Implementation Code
/**
* Adds or overwrites a property on each object in an array using a static value or a callback function.
* Preserves all existing properties of each item.
*
* @param {Array} data.src - The array of objects to process.
* @param {string} data.key - The property name to set on each item.
* @param {string} data.mod - Value mode: "fn" for computed value, "val" for static value.
* @param {Function} [data.fn] - A function that receives the item and returns the value to set (when mod="fn").
* @param {*} [data.val] - Static value to set on each item (when mod="val").
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before processing.
*
* @returns {Array} A new array of objects with the specified property added or overwritten.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const itemSetProperty = function(arr, key, fn, val) {
if (getType(arr) !== 'array') return [];
if (!key) return arr;
const getValue = typeof fn === 'function'
? fn
: function() { return val; };
return arr.map(function(item) {
if (item == null || typeof item !== 'object') return item;
const result = {};
for (const k in item) {
if (item.hasOwnProperty(k)) result[k] = item[k];
}
result[key] = getValue(item);
return result;
});
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// itemSetProperty - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
const fn = data.mod === 'fn' ? data.fn : undefined;
const val = data.mod === 'val' ? data.val : undefined;
return out(itemSetProperty(value, data.key, fn, val));
// ===============================================================================
// itemSetProperty(...) – Apply Mode
// ===============================================================================
/*
return function(arr) {
const fn = data.mod === 'fn' ? data.fn : undefined;
const val = data.mod === 'val' ? data.val : undefined;
return out(itemSetProperty(arr, data.key, fn, val));
};
*/🧪 View Test Scenarios (6 tests)
✅ '[example] Add computed property'
✅ '[example] Set static value'
✅ Overwrite existing property with static value
✅ Preserves all existing properties
✅ Invalid src returns empty array
✅ Invalid fn falls back to static undefined