fix(analytics): add OpenPanel client secret and server-side tracking
Some checks failed
Build and Deploy / build (push) Has been cancelled
Some checks failed
Build and Deploy / build (push) Has been cancelled
- Add OPENPANEL_CLIENT_SECRET for server-side tracking - Add OPENPANEL_API_URL environment variable - Add server-side OpenPanel tracking for orders - Track order_received and revenue events on webhook,description:Commit OpenPanel fixes
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;
|
||||||
@@ -274,6 +282,23 @@ async function handleOrderConfirmed(order: SaleorOrder, eventType: string) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
op.track("revenue", {
|
||||||
|
amount: order.total.gross.amount,
|
||||||
|
currency: order.total.gross.currency,
|
||||||
|
});
|
||||||
|
|
||||||
// Always send admin notification for both ORDER_CREATED and ORDER_CONFIRMED
|
// Always send admin notification for both ORDER_CREATED and ORDER_CONFIRMED
|
||||||
await sendEmailToAdmin({
|
await sendEmailToAdmin({
|
||||||
subject: `🎉 New Order #${order.number} - ${formatPrice(order.total.gross.amount, currency)}`,
|
subject: `🎉 New Order #${order.number} - ${formatPrice(order.total.gross.amount, currency)}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user