Skip to main content

Cart Operations

Track shopping cart interactions to understand user shopping behavior.

Cart Events

type CartEvent =
  | 'Cart Viewed'
  | 'Product Added'
  | 'Remove from Cart'
  | 'Cart Updated';

Basic Cart Tracking

Track Add to Cart

import { track } from '@bilyai/browser';

track('Product Added', {
  products: [{
    id: 'prod_123',
    name: 'Wireless Headphones',
    price: 99.99,
    quantity: 1
  }]
});

Track Remove from Cart

track('Remove from Cart', {
  products: [{
    id: 'prod_123',
    name: 'Wireless Headphones',
    price: 99.99,
    quantity: 1
  }]
});

Track Cart Update

track('Cart Updated', {
  products: [{
    id: 'prod_123',
    name: 'Wireless Headphones',
    price: 99.99,
    quantity: 2  // Updated quantity
  }]
});

React Integration

function AddToCartButton({ product }) {
  const bily = useBily();

  const handleAddToCart = () => {
    bily.track('Product Added', {
      products: [{
        id: product.id,
        name: product.name,
        price: product.price,
        quantity: 1
      }]
    });
  };

  return (
    <button onClick={handleAddToCart}>
      Add to Cart
    </button>
  );
}

Cart Summary Tracking

track('Cart Viewed', {
  products: cartItems.map(item => ({
    id: item.id,
    name: item.name,
    price: item.price,
    quantity: item.quantity
  })),
  order: {
    total: calculateTotal(cartItems),
    currency: 'USD'
  }
});

Best Practices

  1. Track all cart modifications
  2. Include product quantities
  3. Track cart total value
  4. Include relevant metadata

Next Steps