itemMapProperty β GTM Variable Template for Items
itemMapProperty EXTENDED Items
Transforms a single property in every item of an array using a callback function.
When to Use This
Section titled βWhen to Use ThisβGA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Examples
Section titled βExamplesβApply 20% markup to price
INPUT
Items (Array of Objects): [{name: "Shirt", price: 100}, {name: "Hat", price: 50}]
Property Name: price
Transform Function: function(x) { return x * 1.2; }
Property Name: price
Transform Function: function(x) { return x * 1.2; }
OUTPUT
[{name: "Shirt", price: 120}, {name: "Hat", price: 60}]
Convert names to uppercase
INPUT
Items (Array of Objects): [{name: "shirt", color: "blue"}, {name: "hat", color: "red"}]
Property Name: name
Transform Function: function(x) { return x.toUpperCase(); }
Property Name: name
Transform Function: function(x) { return x.toUpperCase(); }
OUTPUT
[{name: "SHIRT", color: "blue"}, {name: "HAT", color: "red"}]
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.
itemMapProperty
Items (Array of Objects)
πΎ Array of objects to transform.
Supported formats:
β Array
Supported formats:
β Array
Property Name
πΎ The property to transform in each object.
Supported formats:
β String
Supported formats:
β String
Transform Function
π― A function that receives the current property value and returns the new value (e.g. x => x * 1.2).
Supported formats:
β Function
Supported formats:
β Function
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
Transform Function function
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemMapProperty()
Related Variables
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Transforms a single property in every object of an array using a callback function. * All other properties are preserved unchanged. * * @param {Array} data.src - Array of objects to transform. * @param {string} data.prp - The property name to transform. * @param {Function} data.fn - A function that receives the property value and returns the new value. * @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} Array of objects with the specified property transformed. * * @framework ggLowCodeGTMKit */const getType = require('getType');
const itemMapProperty = function(items, property, transform) { if (getType(items) !== 'array') return []; if (typeof transform !== 'function') return items;
return items.map(function(item) { if (!item || item[property] === undefined) return item; const result = {}; for (var k in item) { if (item.hasOwnProperty(k)) result[k] = item[k]; } result[property] = transform(item[property]); return result; });};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// itemMapProperty - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(itemMapProperty(value, data.prp, data.fn));
// ===============================================================================// itemMapProperty(...) - Apply Mode// ===============================================================================/*return function(value, property, transform) { property = data.rp1 ? property : data.prp; transform = data.rp2 ? transform : data.fn; return out(itemMapProperty(value, property, transform));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Apply 20% markup to price'β
'[example] Convert names to uppercase'β
Empty array returns emptyβ
Missing property is skippedβ
Non-function transform returns items unchanged