view_cart
The view_cart event fires when a user views their shopping cart. It gives you a snapshot of the full cart at the moment of viewing — every item, every quantity, the total value. This data powers cart-to-checkout conversion rates and helps you understand the average order value composition before checkout begins.
Unlike add_to_cart (which fires when individual items are added), view_cart reflects the complete current state of the cart when the user explicitly reviews it.
When to fire
Section titled “When to fire”Fire view_cart when:
- The cart page or cart drawer renders with cart contents visible
- A cart summary popup/modal opens
- The user navigates back to the cart page
Fire once per cart view, not every time the cart is updated. If the cart updates in real-time (AJAX), you do not need to re-fire unless the user explicitly navigates to the cart view.
Complete dataLayer push
Section titled “Complete dataLayer push”// Always clear previous ecommerce data firstdataLayer.push({ ecommerce: null });
dataLayer.push({ event: 'view_cart', ecommerce: { currency: 'USD', value: 174.47, // sum of (price × quantity) for all items items: [ { item_id: 'SKU-001-BLK-L', item_name: 'Classic Leather Jacket', item_brand: 'Heritage Co.', item_category: 'Apparel', item_category2: 'Outerwear', item_variant: 'Black / Large', price: 89.99, quantity: 1, affiliation: 'Online Store' }, { item_id: 'SKU-047-WHT-M', item_name: 'Cotton Crew T-Shirt', item_brand: 'Heritage Co.', item_category: 'Apparel', item_category2: 'Tops', item_variant: 'White / Medium', price: 24.99, quantity: 2, affiliation: 'Online Store' }, { item_id: 'SKU-112-TAN-34', item_name: 'Woven Leather Belt', item_brand: 'Heritage Co.', item_category: 'Accessories', item_category2: 'Belts', item_variant: 'Tan / 34', price: 34.50, quantity: 1, affiliation: 'Online Store' } ] }});value: 174.47 is calculated as 89.99 × 1 + 24.99 × 2 + 34.50 × 1 = 174.47.
Event schema
Section titled “Event schema”| Parameter | Type | Required | Description |
|---|---|---|---|
| event | string | Required | Must be "view_cart" |
| ecommerce.currency | string | Required | ISO 4217 currency code. All items must be in the same currency. |
| ecommerce.value | number | Required | Total cart value — sum of (price × quantity) for all items. |
| ecommerce.items[] | Array<Item> | Required | All items currently in the cart. |
| items[].item_id | string | Required | SKU or variant-level product identifier. |
| items[].item_name | string | Required | Product name. |
| items[].item_brand | string | Optional | Brand or manufacturer. |
| items[].item_category | string | Optional | Primary product category. |
| items[].item_variant | string | Optional | Selected variant. |
| items[].price | number | Optional | Unit price. Not the line total. |
| items[].quantity | number | Optional | Quantity in cart. |
Calculating cart value
Section titled “Calculating cart value”The value parameter should be the sum of all line totals: Σ(price × quantity) for every item in the cart. This is the pre-tax, pre-shipping cart subtotal.
function calculateCartValue(items) { return items.reduce((total, item) => { return total + (item.price * item.quantity); }, 0);}
// Use with parseFloat to avoid floating-point precision issuesconst cartValue = parseFloat(calculateCartValue(cartItems).toFixed(2));GTM configuration
Section titled “GTM configuration”-
Create a Custom Event trigger. Event name:
view_cart. Name itCE - view_cart. -
Create Data Layer Variables for
ecommerce.currency,ecommerce.value, andecommerce.items. -
Create the GA4 Event tag. Event name:
view_cart. Enable Send Ecommerce data. Addcurrencyandvalueas explicit event parameters. -
Test in Preview mode. Navigate to your cart page with items. Verify all items appear in the items array,
valueequals the correct total, and item-level prices and quantities are correct.
Common mistakes
Section titled “Common mistakes”Omitting items that are out of stock in the cart. Even out-of-stock cart items should be included — the user is seeing them and they’re part of the cart state. Their presence might explain why checkout conversion is lower.
Calculating value as unit price instead of cart total. value must reflect the full cart, not just one item’s price.
Multi-currency carts. If items in the cart have different currencies (rare but possible in marketplaces), standardize to the display currency before pushing.