fix(webhook): prevent duplicate revenue tracking

Move analytics tracking inside ORDER_CONFIRMED conditional block
so revenue is only tracked once when order is confirmed, not twice
(once for ORDER_CREATED and once for ORDER_CONFIRMED).
This commit is contained in:
Unchained
2026-03-25 20:35:39 +02:00
parent eb9a798d40
commit 2e6668ff0d

View File

@@ -163,28 +163,29 @@ async function handleOrderConfirmed(order: Order, eventType: string) {
// Send customer email only for ORDER_CONFIRMED (not ORDER_CREATED)
if (eventType === "ORDER_CONFIRMED") {
await orderNotificationService.sendOrderConfirmation(order);
// Track revenue and order analytics only on ORDER_CONFIRMED (not ORDER_CREATED)
// This prevents duplicate tracking when both events fire for the same order
analyticsService.trackOrderReceived({
orderId: order.id,
orderNumber: order.number,
total: order.total.gross.amount,
currency: order.total.gross.currency,
itemCount,
customerEmail: order.userEmail,
eventType,
});
analyticsService.trackRevenue({
amount: order.total.gross.amount,
currency: order.total.gross.currency,
orderId: order.id,
orderNumber: order.number,
});
}
// Send admin notification for both events
await orderNotificationService.sendOrderConfirmationToAdmin(order);
// Track analytics (fire and forget - don't await)
analyticsService.trackOrderReceived({
orderId: order.id,
orderNumber: order.number,
total: order.total.gross.amount,
currency: order.total.gross.currency,
itemCount,
customerEmail: order.userEmail,
eventType,
});
analyticsService.trackRevenue({
amount: order.total.gross.amount,
currency: order.total.gross.currency,
orderId: order.id,
orderNumber: order.number,
});
}
async function handleOrderFulfilled(order: Order) {