itemSet β GTM Variable Template for Items
itemSet CORE Items
Adds or overwrites a property on each object in an array.
When to Use This
Section titled βWhen to Use ThisβGA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Examples
Section titled βExamplesβ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; }
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
Property Name: currency
Value or Function: EUR
OUTPUT
[{price: 10, currency: "EUR"}, {price: 20, currency: "EUR"}]
Live Sandbox
Section titled βLive Sandboxβ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
Supported formats:
β Array
Property Name
πΎ The property name to add or overwrite on each item.
Supported formats:
β String
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)
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemSetProperty()
Related Variables
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled β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 (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