Lesson 3 of 8
Arrays, Objects & Loops
GA4 ecommerce is just arrays and objects, so these are the most important structures you will touch. An object is a labelled bag of values; an array is an ordered list.
const item = { item_id: "AUR-WH-09", item_name: "Aurora Headphones", price: 129 };
item.price; // 129 (dot access)
item["item_name"]; // "Aurora Headphones" (bracket access)
const cart = [item, { item_id: "CBL-USB", price: 9 }]; // array of objects
cart.length; // 2
cart[0].item_id; // "AUR-WH-09"Looping to transform a list
You will constantly turn one list into another shape. .map() is the workhorse: it returns a new array with each item transformed.
// Site cart → GA4 items array
const items = cart.map(function (line) {
return {
item_id: line.sku,
item_name: line.title,
price: Number(line.price),
quantity: line.qty,
};
});.filter() keeps a subset, and .reduce() collapses a list to one value (like a cart total), but .map() covers the majority of tagging work.
Key takeaway
Objects hold labelled values; arrays hold lists. .map() reshapes a list of one structure into another, which is exactly what GA4 ecommerce demands.