Sub topic 7
Cart Events: `add_to_cart`, `remove_from_cart`, and `view_cart`
This chapter assumes you understand GA4 events, JavaScript objects and arrays, and how a shop represents its cart. It focuses on modeling cart changes consistently so GA4 can calculate item and revenue metrics correctly.
The cart event model
GA4 uses three recommended events for cart activity:
| Event | When to send it | What the items represent |
|---|---|---|
| `add_to_cart` | A shopper adds products to the cart | Products added in that action |
| `remove_from_cart` | A shopper removes products from the cart | Products removed in that action |
| `view_cart` | A shopper opens or views the cart | All products currently in the cart |
Each event normally includes an items array. Each item should contain a stable item_id or item_name, plus its unit price and quantity. The event-level value should equal the value represented by the event's items:
value = sum(price × quantity)
Send currency with value, using an ISO 4217 code such as USD or EUR.
gtag('event', 'add_to_cart', {
currency: 'USD',
value: 49.98,
items: [
{
item_id: 'SKU_1001',
item_name: 'Trail Running Shoe',
item_brand: 'Acme',
item_category: 'Shoes',
item_variant: 'blue',
price: 24.99,
quantity: 2
}
]
});For remove_from_cart, report the products removed, not the cart's remaining contents:
gtag('event', 'remove_from_cart', {
currency: 'USD',
value: 24.99,
items: [
{
item_id: 'SKU_1001',
item_name: 'Trail Running Shoe',
price: 24.99,
quantity: 1
}
]
});For view_cart, report the complete current cart:
gtag('event', 'view_cart', {
currency: 'USD',
value: 74.97,
items: [
{
item_id: 'SKU_1001',
item_name: 'Trail Running Shoe',
price: 24.99,
quantity: 2
},
{
item_id: 'SKU_2002',
item_name: 'Running Socks',
price: 24.99,
quantity: 1
}
]
});Avoiding duplicate and incorrect events
Send add_to_cart after the cart accepts the addition. Do not send it merely because a product page button was clicked if the request later fails. Similarly, send remove_from_cart only after the removal succeeds.
A common error is sending the entire cart in every add_to_cart event. That inflates item quantities and makes the event's value misleading. Send only the newly added lines for add_to_cart and only the removed lines for remove_from_cart.
The view_cart event describes a snapshot. Recalculate its items and value from the current cart instead of reusing an earlier object.
Exercise
A shopper has two units of a $10 product in the cart and removes one. Which quantity and value should the removefromcart event report?
Show answer
It should report quantity 1 and value 10 in the shop's currency. The items array should contain the removed unit, not the remaining cart.