Skip to content

itemMapProperty β€” GTM Variable Template for Items

VARIABLES β€Ί ITEMS
itemMapProperty EXTENDED Items
Direct (.tpl) Apply (.tpl)

Transforms a single property in every item of an array using a callback function.


GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.

Type Conversion

Safely convert between data types β€” strings, numbers, booleans, arrays, objects.


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; }
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(); }
OUTPUT
[{name: "SHIRT", color: "blue"}, {name: "HAT", color: "red"}]

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
Property Name
πŸ’Ύ The property to transform in each object.

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
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
itemMapProperty()


πŸ“œ 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