|
1 | 1 | import { Link } from "react-router"; |
2 | 2 | import { useCart, type CartItem } from "../context/CartContext"; |
| 3 | +import { usePostHog } from "@posthog/react"; |
3 | 4 |
|
4 | 5 | export default function Cart() { |
5 | 6 | const { cart, removeFromCart, updateQuantity, getCartTotal } = useCart(); |
| 7 | + const posthog = usePostHog(); |
6 | 8 |
|
7 | 9 | const handleRemoveFromCart = (item: CartItem) => { |
8 | 10 | removeFromCart(item.id); |
| 11 | + posthog?.capture("product_removed_from_cart", { |
| 12 | + product_id: item.id, |
| 13 | + product_name: item.name, |
| 14 | + product_price: item.price, |
| 15 | + product_category: item.category, |
| 16 | + quantity: item.quantity, |
| 17 | + }); |
9 | 18 | }; |
10 | 19 |
|
11 | 20 | const handleUpdateQuantity = (item: CartItem, newQuantity: number) => { |
12 | 21 | updateQuantity(item.id, newQuantity); |
| 22 | + posthog?.capture("cart_quantity_updated", { |
| 23 | + product_id: item.id, |
| 24 | + product_name: item.name, |
| 25 | + old_quantity: item.quantity, |
| 26 | + new_quantity: newQuantity, |
| 27 | + }); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleCheckoutClick = () => { |
| 31 | + posthog?.capture("checkout_started", { |
| 32 | + cart_total: getCartTotal(), |
| 33 | + cart_items_count: cart.length, |
| 34 | + }); |
| 35 | + }; |
| 36 | + |
| 37 | + const handleContinueShoppingClick = () => { |
| 38 | + posthog?.capture("continue_shopping_clicked", { |
| 39 | + cart_total: getCartTotal(), |
| 40 | + cart_items_count: cart.length, |
| 41 | + }); |
13 | 42 | }; |
14 | 43 |
|
15 | 44 | if (cart.length === 0) { |
@@ -154,13 +183,15 @@ export default function Cart() { |
154 | 183 |
|
155 | 184 | <Link |
156 | 185 | to="/checkout" |
| 186 | + onClick={handleCheckoutClick} |
157 | 187 | className="block w-full bg-indigo-600 text-white py-3 rounded-lg text-center font-semibold hover:bg-indigo-700 transition" |
158 | 188 | > |
159 | 189 | Proceed to Checkout |
160 | 190 | </Link> |
161 | 191 |
|
162 | 192 | <Link |
163 | 193 | to="/products" |
| 194 | + onClick={handleContinueShoppingClick} |
164 | 195 | className="block w-full mt-3 bg-gray-200 text-gray-900 py-3 rounded-lg text-center font-semibold hover:bg-gray-300 transition" |
165 | 196 | > |
166 | 197 | Continue Shopping |
|
0 commit comments