Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add event name to sales tracking

Kiran K5c7ca7a67b93
diff --git a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
index 54902b078a..63e4c06585 100644
--- a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
@@ -62,6 +62,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
recordSale({
...leadEvent.data[0],
event_id: nanoid(16),
+ event_name: "Subscription created",
payment_processor: "stripe",
amount: charge.amount_total!,
currency: charge.currency!,
diff --git a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
index dc9ec183e5..37d77e2f20 100644
--- a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
@@ -47,6 +47,7 @@ export async function invoicePaid(event: Stripe.Event) {
recordSale({
...leadEvent.data[0],
event_id: nanoid(16),
+ event_name: "Invoice paid",
payment_processor: "stripe",
amount: invoice.amount_paid,
currency: invoice.currency,
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 2e5686e6cf..3786f845fe 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -23,6 +23,7 @@ export const POST = withWorkspaceEdge(
amount,
currency,
metadata,
+ eventName,
} = trackSaleRequestSchema.parse(await parseRequestBody(req));
// Find customer
@@ -60,6 +61,7 @@ export const POST = withWorkspaceEdge(
recordSale({
...clickData,
event_id: nanoid(16),
+ event_name: eventName,
customer_id: customer.id,
payment_processor: paymentProcessor,
amount,
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index 814b6745a8..a6d297fa89 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -21,6 +21,15 @@ export const trackSaleRequestSchema = z.object({
.describe("The payment processor via which the sale was made."),
// Optional
+ eventName: z
+ .string()
+ .max(50)
+ .optional()
+ .default("Purchase")
+ .describe(
+ "The name of the sale event. It can be used to track different types of event for example 'Purchase', 'Subscription', 'Upgrade', etc.",
+ )
+ .openapi({ example: "Purchase" }),
invoiceId: z
.string()
.nullish()
@@ -53,6 +62,7 @@ export const saleEventSchemaTB = clickEventSchemaTB
timestamp: z.string().optional(), //autogenerated by Tinybird
event_id: z.string(),
customer_id: z.string(),
+ event_name: z.string().default("Purchase"),
payment_processor: z.string(),
amount: z.number(),
invoice_id: z.string().default(""),