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:
@@ -163,28 +163,29 @@ async function handleOrderConfirmed(order: Order, eventType: string) {
|
|||||||
// Send customer email only for ORDER_CONFIRMED (not ORDER_CREATED)
|
// Send customer email only for ORDER_CONFIRMED (not ORDER_CREATED)
|
||||||
if (eventType === "ORDER_CONFIRMED") {
|
if (eventType === "ORDER_CONFIRMED") {
|
||||||
await orderNotificationService.sendOrderConfirmation(order);
|
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
|
// Send admin notification for both events
|
||||||
await orderNotificationService.sendOrderConfirmationToAdmin(order);
|
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) {
|
async function handleOrderFulfilled(order: Order) {
|
||||||
|
|||||||
Reference in New Issue
Block a user