Skip to content

itemUniqByProperty β€” GTM Variable Template for Items

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

Removes duplicate objects from an array based on a property value. Keeps the first occurrence.


GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.

Filtering

Select or exclude items from collections based on criteria or predicates.


Deduplicate by item_id
INPUT
Items (Array of Objects): [{item_id: "A", price: 10}, {item_id: "B", price: 20}, {item_id: "A", price: 30}]
Property Name: item_id
OUTPUT
[{item_id: "A", price: 10}, {item_id: "B", price: 20}]
No duplicates
INPUT
Items (Array of Objects): [{name: "Shirt", price: 10}, {name: "Hat", price: 20}]
Property Name: name
OUTPUT
[{name: "Shirt", price: 10}, {name: "Hat", price: 20}]

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

itemUniqByProperty
Items (Array of Objects)
πŸ’Ύ Array of objects to deduplicate.

Supported formats:
  βœ“ Array
Property Name
πŸ’Ύ The property to check for uniqueness (e.g. item_id, item_name).

Supported formats:
  βœ“ String
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
itemUniqByProperty()


πŸ“œ View Implementation Code
/**
* Removes duplicate objects from an array based on a property value.
* Keeps the first occurrence of each unique value.
*
* @param {Array} data.src - Array of objects to deduplicate.
* @param {string} data.prp - The property to check for uniqueness.
* @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 with duplicates removed based on the specified property.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const itemUniqByProperty = function(items, property) {
if (getType(items) !== 'array') return [];
if (typeof property !== 'string') return items;
const seen = {};
const result = [];
for (var i = 0; i < items.length; i++) {
const item = items[i];
if (item == null || typeof item !== 'object') continue;
const key = item[property];
const keyStr = typeof key === 'undefined' ? '__undefined__' : '' + key;
if (!seen.hasOwnProperty(keyStr)) {
seen[keyStr] = true;
result.push(item);
}
}
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// itemUniqByProperty - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(itemUniqByProperty(value, data.prp));
// ===============================================================================
// itemUniqByProperty(...) - Apply Mode
// ===============================================================================
/*
return function(value, property) {
property = data.rp1 ? property : data.prp;
return out(itemUniqByProperty(value, property));
};
*/
πŸ§ͺ View Test Scenarios (7 tests)
βœ… '[example] Deduplicate by item_id'
βœ… '[example] No duplicates'
βœ… All duplicates - keeps first only
βœ… Empty array returns empty
βœ… Null src returns empty array
βœ… Items with missing property are kept (once)
βœ… Numeric string coercion - treats 1 and 1-string as same