fix(analytics): add OpenPanel revenue tracking to webhooks
Add OpenPanel import and initialization that was missing from webhook route. Add order_received and revenue tracking when orders are confirmed. Revenue tracking uses op.revenue() method with amount, currency, order_id, and order_number properties.
This commit is contained in:
@@ -5,10 +5,18 @@ import { OrderConfirmation } from "@/emails/OrderConfirmation";
|
|||||||
import { OrderShipped } from "@/emails/OrderShipped";
|
import { OrderShipped } from "@/emails/OrderShipped";
|
||||||
import { OrderCancelled } from "@/emails/OrderCancelled";
|
import { OrderCancelled } from "@/emails/OrderCancelled";
|
||||||
import { OrderPaid } from "@/emails/OrderPaid";
|
import { OrderPaid } from "@/emails/OrderPaid";
|
||||||
|
import { OpenPanel } from "@openpanel/nextjs";
|
||||||
|
|
||||||
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || "https://dev.manoonoils.com";
|
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || "https://dev.manoonoils.com";
|
||||||
const DASHBOARD_URL = process.env.DASHBOARD_URL || "https://dashboard.manoonoils.com";
|
const DASHBOARD_URL = process.env.DASHBOARD_URL || "https://dashboard.manoonoils.com";
|
||||||
|
|
||||||
|
// Initialize OpenPanel for server-side tracking
|
||||||
|
const op = new OpenPanel({
|
||||||
|
clientId: process.env.NEXT_PUBLIC_OPENPANEL_CLIENT_ID || "",
|
||||||
|
clientSecret: process.env.OPENPANEL_CLIENT_SECRET || "",
|
||||||
|
apiUrl: process.env.OPENPANEL_API_URL || "https://op.nodecrew.me/api",
|
||||||
|
});
|
||||||
|
|
||||||
interface SaleorWebhookHeaders {
|
interface SaleorWebhookHeaders {
|
||||||
"saleor-event": string;
|
"saleor-event": string;
|
||||||
"saleor-domain": string;
|
"saleor-domain": string;
|
||||||
@@ -295,6 +303,24 @@ async function handleOrderConfirmed(order: SaleorOrder, eventType: string) {
|
|||||||
eventType: "ORDER_CONFIRMED",
|
eventType: "ORDER_CONFIRMED",
|
||||||
orderId: order.id,
|
orderId: order.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Track order in OpenPanel
|
||||||
|
op.track("order_received", {
|
||||||
|
order_id: order.id,
|
||||||
|
order_number: order.number,
|
||||||
|
total: order.total.gross.amount,
|
||||||
|
currency: order.total.gross.currency,
|
||||||
|
item_count: order.lines.reduce((sum, line) => sum + line.quantity, 0),
|
||||||
|
customer_email: customerEmail,
|
||||||
|
event_type: eventType,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Track revenue using OpenPanel's revenue method
|
||||||
|
op.revenue(order.total.gross.amount, {
|
||||||
|
currency: order.total.gross.currency,
|
||||||
|
order_id: order.id,
|
||||||
|
order_number: order.number,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleOrderFulfilled(order: SaleorOrder) {
|
async function handleOrderFulfilled(order: SaleorOrder) {
|
||||||
|
|||||||
Reference in New Issue
Block a user