itemSplitProperty β GTM Variable Template for Items
itemSplitProperty EXTENDED Items
Splits a breadcrumb category string into GA4's flat item_category structure.
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βExpand breadcrumb categories
INPUT
Items (Array of Objects): [{item_name: "Blue T-Shirt", item_category: "Clothing>Men>T-Shirts"}]
Category Property: item_category
Separator: >
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: >
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"}]
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.
itemSplitProperty
Items (Array of Objects)
πΎ Array of e-commerce items to process.
Supported formats:
β Array
Supported formats:
β Array
Category Property
πΎ The property containing the breadcrumb string to expand (default: item_category).
Supported formats:
β String
Supported formats:
β String
Separator
πΎ The delimiter used in the breadcrumb string (default: >).
Common separators:
β > (Shopify, WooCommerce)
β / (Magento)
β | (custom)
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemSplitProperty()
Related Variables
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled βUnder the Hoodβπ 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