Skip to content

itemSplitProperty β€” GTM Variable Template for Items

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

Splits a breadcrumb category string into GA4's flat item_category structure.


GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.

Type Conversion

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


Expand breadcrumb categories
INPUT
Items (Array of Objects): [{item_name: "Blue T-Shirt", item_category: "Clothing>Men>T-Shirts"}]
Category Property: item_category
Separator: >
OUTPUT
[{item_name: "Blue T-Shirt", item_category: "Clothing", item_category2: "Men", item_category3: "T-Shirts"}]
Multiple items with different depths
INPUT
Items (Array of Objects): [{item_name: "Shirt", item_category: "Clothing>Men"}, {item_name: "Laptop", item_category: "Electronics>Computers>Laptops>Gaming>Premium"}]
Category Property: item_category
Separator: >
OUTPUT
[{item_name: "Shirt", item_category: "Clothing", item_category2: "Men"}, {item_name: "Laptop", item_category: "Electronics", item_category2: "Computers", item_category3: "Laptops", item_category4: "Gaming", item_category5: "Premium"}]

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

itemSplitProperty
Items (Array of Objects)
πŸ’Ύ Array of e-commerce items to process.

Supported formats:
  βœ“ Array
Category Property
πŸ’Ύ The property containing the breadcrumb string to expand (default: item_category).

Supported formats:
  βœ“ String
Separator
πŸ’Ύ The delimiter used in the breadcrumb string (default: >).

Common separators:
  βœ“ > (Shopify, WooCommerce)
  βœ“ / (Magento)
  βœ“ | (custom)
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
Category Property string
Separator string
itemSplitProperty()


πŸ“œ View Implementation Code
/**
* Splits a breadcrumb category string into GA4's flat item_category structure.
* GA4 supports item_category through item_category5 (5 levels max).
*
* @param {Array} data.src - Array of e-commerce items.
* @param {string} data.prp - The property containing the breadcrumb string (default: "item_category").
* @param {string} data.sep - The delimiter used in the breadcrumb (default: ">").
* @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} Items with the specified property split into numbered properties.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const itemSplitProperty = function(items, property, separator) {
if (getType(items) !== 'array') return [];
property = property || 'item_category';
separator = separator || '>';
return items.map(function(item) {
if (item == null || typeof item !== 'object') return item;
const catValue = item[property];
if (typeof catValue !== 'string' || catValue.indexOf(separator) === -1) return item;
const parts = catValue.split(separator);
const result = {};
for (var k in item) {
if (item.hasOwnProperty(k)) result[k] = item[k];
}
for (var i = 0; i < parts.length; i++) {
var trimmed = typeof parts[i] === 'string' ? parts[i].trim() : '';
if (trimmed) result[i === 0 ? property : property + (i + 1)] = trimmed;
}
return result;
});
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// itemSplitProperty - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(itemSplitProperty(value, data.prp, data.sep));
// ===============================================================================
// itemSplitProperty(...) - Apply Mode
// ===============================================================================
/*
return function(value, property) {
property = data.rp1 ? property : data.prp;
return out(itemSplitProperty(value, property, data.sep));
};
*/
πŸ§ͺ View Test Scenarios (8 tests)
βœ… '[example] Expand breadcrumb categories'
βœ… '[example] Multiple items with different depths'
βœ… No separator in value - returns item unchanged
βœ… Slash separator (Magento style)
βœ… Trims whitespace from parts
βœ… Handles any depth dynamically
βœ… Empty array returns empty
βœ… Missing category property - returns items unchanged