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.

allow passing leadEventName in /track/sale

Steven Tey15cb6ef11a3f
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 9c5e991d64..ae99611cf3 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -41,8 +41,9 @@ export const POST = withWorkspaceEdge(
});
}
- // deduplicate lead events – only record 1 event per hour
- const { success } = await ratelimit(1, "1 h").limit(
+ // deduplicate lead events – only record 1 unique event per 7 days
+ // TODO: maybe convert to redis.set nx like how we're doing it for invoice deduplication?
+ const { success } = await ratelimit(1, "7 d").limit(
`recordLead:${workspace.id}:${customerExternalId}:${eventName.toLowerCase().replace(" ", "-")}`,
);
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 5b4f45d740..5b8f095dca 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -32,6 +32,7 @@ export const POST = withWorkspaceEdge(
currency,
metadata,
eventName,
+ leadEventName,
} = trackSaleRequestSchema.parse(await parseRequestBody(req));
if (invoiceId) {
@@ -77,8 +78,11 @@ export const POST = withWorkspaceEdge(
});
}
- // Find lead
- const leadEvent = await getLeadEvent({ customerId: customer.id });
+ // Find lead event
+ const leadEvent = await getLeadEvent({
+ customerId: customer.id,
+ eventName: leadEventName,
+ });
if (!leadEvent || leadEvent.data.length === 0) {
throw new DubApiError({
diff --git a/apps/web/lib/tinybird/get-lead-event.ts b/apps/web/lib/tinybird/get-lead-event.ts
index 29c616fdbf..178cdb32df 100644
--- a/apps/web/lib/tinybird/get-lead-event.ts
+++ b/apps/web/lib/tinybird/get-lead-event.ts
@@ -6,6 +6,7 @@ export const getLeadEvent = tb.buildPipe({
pipe: "get_lead_event",
parameters: z.object({
customerId: z.string(),
+ eventName: z.string().nullish(),
}),
data: leadEventSchemaTB,
});
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index 969d4fbd54..ea087a033e 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -55,6 +55,11 @@ export const trackSaleRequestSchema = z.object({
.nullish()
.default(null)
.describe("Additional metadata to be stored with the sale event."),
+ leadEventName: z
+ .string()
+ .nullish()
+ .default(null)
+ .describe("The name of the lead event that occurred before the sale."),
});
export const trackSaleResponseSchema = z.object({