itemUniqByProperty β GTM Variable Template for Items
itemUniqByProperty EXTENDED Items
Removes duplicate objects from an array based on a property value. Keeps the first occurrence.
When to Use This
Section titled βWhen to Use ThisβGA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Filtering
Select or exclude items from collections based on criteria or predicates.
Examples
Section titled βExamplesβ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
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
Property Name: name
OUTPUT
[{name: "Shirt", price: 10}, {name: "Hat", price: 20}]
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.
itemUniqByProperty
Items (Array of Objects)
πΎ Array of objects to deduplicate.
Supported formats:
β Array
Supported formats:
β Array
Property Name
πΎ The property to check for uniqueness (e.g. item_id, item_name).
Supported formats:
β String
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemUniqByProperty()
Related Variables
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled βUnder the Hoodβπ 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