Skip to content

itemSet β€” GTM Variable Template for Items

VARIABLES β€Ί ITEMS
itemSet CORE Items
Direct (.tpl) Apply (.tpl)

Adds or overwrites a property on each object in an array.


GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.


Add computed property
INPUT
Items (Array of Objects): [{price: 10, quantity: 2}, {price: 20, quantity: 3}]
Property Name: total
Value or Function: function(item) { return item.price * item.quantity; }
OUTPUT
[{price: 10, quantity: 2, total: 20}, {price: 20, quantity: 3, total: 60}]
Set static value
INPUT
Items (Array of Objects): [{price: 10}, {price: 20}]
Property Name: currency
Value or Function: EUR
OUTPUT
[{price: 10, currency: "EUR"}, {price: 20, currency: "EUR"}]

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

itemSet
Items (Array of Objects)
πŸ’Ύ Array of objects to process.

Supported formats:
  βœ“ Array
Property Name
πŸ’Ύ The property name to add or overwrite on each item.

Supported formats:
  βœ“ String
Value or Function
🎯 The value to set on each item, or a function that receives the item and returns the value (e.g. item => item.price * item.quantity).

Supported formats:
  βœ“ Any (static value set on every item)
  βœ“ Function (receives each item, returns value)
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.
Items (Array of Objects) array
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Property Name string
Value or Function any
itemSetProperty()


πŸ“œ View Implementation Code
/**
* Adds or overwrites a property on each object in an array using a static value or a callback function (auto-detected).
* Preserves all existing properties of each item.
*
* @param {Array} data.src - Array of objects to process.
* @param {string} data.prp - Property name to set on each item.
* @param {any|Function} [data.val] - The value to set, or a function that receives the item and returns the value (e.g. item => item.price * item.quantity).
* @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(items, property, valueOrFn) {
if (getType(items) !== 'array') return [];
if (!property) return items;
const getValue = typeof valueOrFn === 'function'
? valueOrFn
: function() { return valueOrFn; };
return items.map(function(item) {
if (item == null || typeof item !== 'object') return item;
const result = {};
for (var k in item) {
if (item.hasOwnProperty(k)) result[k] = item[k];
}
result[property] = getValue(item);
return result;
});
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// itemSet - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(itemSetProperty(value, data.prp, data.val));
// ===============================================================================
// itemSet(...) - Apply Mode
// ===============================================================================
/*
return function(value, property, valueOrFn) {
property = data.rp1 ? property : data.prp;
valueOrFn = data.rp2 ? valueOrFn : data.val;
return out(itemSetProperty(value, property, valueOrFn));
};
*/
πŸ§ͺ View Test Scenarios (6 tests)
βœ… '[example] Add computed property'
βœ… '[example] Set static value'
βœ… Overwrite existing property
βœ… Preserves all existing properties
βœ… Empty array returns empty
βœ… Invalid src returns empty array