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.

Fix track endpoint efficiency

Steven Tey0e9664f94f92
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index ae90ad15a4..6b27e202aa 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -87,68 +87,78 @@ export const POST = withWorkspaceEdge(
});
}
- const [_lead, _customer, link, _project] = await Promise.all([
- recordLead({
- ...clickData,
- event_id: nanoid(16),
- event_name: eventName,
- customer_id: customer.id,
- metadata: metadata ? JSON.stringify(metadata) : "",
- }),
-
- recordCustomer({
- workspace_id: workspace.id,
- customer_id: customer.id,
- name: customer.name || "",
- email: customer.email || "",
- avatar: customer.avatar || "",
- }),
-
- // update link leads count
- prismaEdge.link.update({
- where: {
- id: clickData.link_id,
- },
- data: {
- leads: {
- increment: 1,
- },
- },
- }),
- prismaEdge.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- usage: {
- increment: 1,
- },
- },
- }),
- ]);
-
- const lead = transformLeadEventData({
- ...clickData,
- link,
- eventName,
- customerId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
- });
-
waitUntil(
- sendLinkWebhookOnEdge({
- trigger: "lead.created",
- linkId: link.id,
- data: lead,
- }),
+ (async () => {
+ const [_lead, _customer, link, _project] = await Promise.all([
+ recordLead({
+ ...clickData,
+ event_id: nanoid(16),
+ event_name: eventName,
+ customer_id: customer.id,
+ metadata: metadata ? JSON.stringify(metadata) : "",
+ }),
+
+ recordCustomer({
+ workspace_id: workspace.id,
+ customer_id: customer.id,
+ name: customer.name || "",
+ email: customer.email || "",
+ avatar: customer.avatar || "",
+ }),
+
+ // update link leads count
+ prismaEdge.link.update({
+ where: {
+ id: clickData.link_id,
+ },
+ data: {
+ leads: {
+ increment: 1,
+ },
+ },
+ }),
+ prismaEdge.project.update({
+ where: {
+ id: workspace.id,
+ },
+ data: {
+ usage: {
+ increment: 1,
+ },
+ },
+ }),
+ ]);
+
+ const lead = transformLeadEventData({
+ ...clickData,
+ link,
+ eventName,
+ customerId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ });
+
+ await sendLinkWebhookOnEdge({
+ trigger: "lead.created",
+ linkId: link.id,
+ data: lead,
+ });
+ })(),
);
return NextResponse.json({
- ...lead,
+ click: {
+ id: clickId,
+ },
+ customer: {
+ id: customer.externalId,
+ name: customer.name,
+ email: customer.email,
+ avatar: customer.avatar,
+ },
// for backwards compatibility – will remove soon
- clickId: lead.click.id,
+ clickId,
customerId: customer.externalId,
customerName: customer.name,
customerEmail: customer.email,
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index e9f6a53711..a8c714f861 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -57,71 +57,95 @@ export const POST = withWorkspaceEdge(
.omit({ timestamp: true })
.parse(leadEvent.data[0]);
- const [_sale, link, _project] = await Promise.all([
- recordSale({
- ...clickData,
- event_id: nanoid(16),
- event_name: eventName,
- customer_id: customer.id,
- payment_processor: paymentProcessor,
- amount,
- currency,
- invoice_id: invoiceId || "",
- metadata: metadata ? JSON.stringify(metadata) : "",
- }),
- // update link sales count
- prismaEdge.link.update({
- where: {
- id: clickData.link_id,
- },
- data: {
- sales: {
- increment: 1,
- },
- saleAmount: {
- increment: amount,
- },
- },
- }),
- // update workspace sales usage
- prismaEdge.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- usage: {
- increment: 1,
- },
- salesUsage: {
- increment: amount,
- },
- },
- }),
- ]);
+ waitUntil(
+ (async () => {
+ const [_sale, link, _project] = await Promise.all([
+ recordSale({
+ ...clickData,
+ event_id: nanoid(16),
+ event_name: eventName,
+ customer_id: customer.id,
+ payment_processor: paymentProcessor,
+ amount,
+ currency,
+ invoice_id: invoiceId || "",
+ metadata: metadata ? JSON.stringify(metadata) : "",
+ }),
+ // update link sales count
+ prismaEdge.link.update({
+ where: {
+ id: clickData.link_id,
+ },
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: amount,
+ },
+ },
+ }),
+ // update workspace sales usage
+ prismaEdge.project.update({
+ where: {
+ id: workspace.id,
+ },
+ data: {
+ usage: {
+ increment: 1,
+ },
+ salesUsage: {
+ increment: amount,
+ },
+ },
+ }),
+ ]);
+
+ const sale = transformSaleEventData({
+ ...clickData,
+ link,
+ eventName,
+ paymentProcessor,
+ invoiceId,
+ amount,
+ currency,
+ customerId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ });
+
+ await sendLinkWebhookOnEdge({
+ trigger: "sale.created",
+ linkId: link.id,
+ data: sale,
+ });
+ })(),
+ );
- const sale = transformSaleEventData({
- ...clickData,
- link,
+ return NextResponse.json({
eventName,
- paymentProcessor,
- invoiceId,
+ customer: {
+ id: customer.externalId,
+ name: customer.name,
+ email: customer.email,
+ avatar: customer.avatar,
+ },
+ sale: {
+ amount,
+ currency,
+ invoiceId,
+ paymentProcessor,
+ metadata,
+ },
+ // for backwards compatibility – will remove soon
+ customerId: externalId,
amount,
currency,
- customerId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
+ invoiceId,
+ paymentProcessor,
+ metadata,
});
-
- waitUntil(
- sendLinkWebhookOnEdge({
- trigger: "sale.created",
- linkId: link.id,
- data: sale,
- }),
- );
-
- return NextResponse.json(sale);
},
{
requiredAddOn: "conversion",
diff --git a/apps/web/lib/openapi/track/lead.ts b/apps/web/lib/openapi/track/lead.ts
index 2e31b3140f..f35af72bd4 100644
--- a/apps/web/lib/openapi/track/lead.ts
+++ b/apps/web/lib/openapi/track/lead.ts
@@ -1,6 +1,8 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import { leadEventSchema } from "@/lib/webhook/schemas";
-import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";
+import {
+ trackLeadRequestSchema,
+ trackLeadResponseSchema,
+} from "@/lib/zod/schemas/leads";
import { ZodOpenApiOperationObject } from "zod-openapi";
export const trackLead: ZodOpenApiOperationObject = {
@@ -20,7 +22,7 @@ export const trackLead: ZodOpenApiOperationObject = {
description: "A lead was tracked.",
content: {
"application/json": {
- schema: leadEventSchema,
+ schema: trackLeadResponseSchema,
},
},
},
diff --git a/apps/web/lib/openapi/track/sale.ts b/apps/web/lib/openapi/track/sale.ts
index 1719da3933..0102bde7d2 100644
--- a/apps/web/lib/openapi/track/sale.ts
+++ b/apps/web/lib/openapi/track/sale.ts
@@ -1,6 +1,8 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import { saleEventSchema } from "@/lib/webhook/schemas";
-import { trackSaleRequestSchema } from "@/lib/zod/schemas/sales";
+import {
+ trackSaleRequestSchema,
+ trackSaleResponseSchema,
+} from "@/lib/zod/schemas/sales";
import { ZodOpenApiOperationObject } from "zod-openapi";
export const trackSale: ZodOpenApiOperationObject = {
@@ -20,7 +22,7 @@ export const trackSale: ZodOpenApiOperationObject = {
description: "A sale was tracked.",
content: {
"application/json": {
- schema: saleEventSchema,
+ schema: trackSaleResponseSchema,
},
},
},
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index 69292c7f74..c27bc81bc3 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -6,6 +6,7 @@ import {
nanoid,
} from "@dub/utils";
import { EU_COUNTRY_CODES } from "@dub/utils/src/constants/countries";
+import { Link } from "@prisma/client";
import { ipAddress } from "@vercel/edge";
import { NextRequest, userAgent } from "next/server";
import {
@@ -18,7 +19,10 @@ import { conn } from "../planetscale";
import { ratelimit } from "../upstash";
import { webhookCache } from "../webhook/cache";
import { sendWebhooks } from "../webhook/qstash";
-import { transformClickEventData } from "../webhook/transform";
+import {
+ transformClickEventData,
+ transformLinkEventData,
+} from "../webhook/transform";
/**
* Recording clicks with geo, ua, referer and timestamp data
@@ -153,16 +157,21 @@ export async function recordClick({
);
if (linkWebhooks.length > 0) {
+ const link = (await conn
+ .execute("SELECT * FROM Link WHERE id = ?", [linkId])
+ .then((res) => res.rows[0])) as Link;
+
await sendWebhooks({
trigger: "link.clicked",
webhooks: linkWebhooks,
- // @ts-ignore
- data: transformClickEventData({
- ...clickData,
- link: {
- id: linkId,
- },
- }),
+ data: {
+ ...transformClickEventData({
+ ...clickData,
+ bot: clickData.bot ? 1 : 0,
+ qr: clickData.qr ? 1 : 0,
+ }),
+ link: transformLinkEventData(link),
+ },
});
}
}
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index a3b9f25a5e..2e8d7558f8 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -38,18 +38,13 @@ export const transformLinkEventData = (data: Link) => {
};
export const transformClickEventData = (
- data: z.infer<typeof clickEventSchemaTB> & {
- link: any;
- },
+ data: z.infer<typeof clickEventSchemaTB>,
) => {
const click = Object.fromEntries(
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
- return clickEventSchema.parse({
- ...click,
- link: transformLinkEventData(transformLink(data.link as LinkWithTags)),
- });
+ return clickEventSchema.parse(click);
};
export const transformLeadEventData = (data: any) => {
diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.ts
index 6c58e976de..35d6e9a523 100644
--- a/apps/web/lib/zod/schemas/leads.ts
+++ b/apps/web/lib/zod/schemas/leads.ts
@@ -49,6 +49,18 @@ export const trackLeadRequestSchema = z.object({
.describe("Additional metadata to be stored with the lead event"),
});
+export const trackLeadResponseSchema = z.object({
+ click: z.object({
+ id: z.string(),
+ }),
+ customer: z.object({
+ id: z.string(),
+ name: z.string(),
+ email: z.string(),
+ avatar: z.string(),
+ }),
+});
+
export const leadEventSchemaTB = clickEventSchemaTB
.omit({ timestamp: true })
.and(
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index 02f73e66d9..3401a9236e 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -46,6 +46,23 @@ export const trackSaleRequestSchema = z.object({
.describe("Additional metadata to be stored with the sale event."),
});
+export const trackSaleResponseSchema = z.object({
+ eventName: z.string(),
+ customer: z.object({
+ id: z.string(),
+ name: z.string(),
+ email: z.string(),
+ avatar: z.string(),
+ }),
+ sale: z.object({
+ amount: z.number(),
+ currency: z.string(),
+ invoiceId: z.string().nullable(),
+ paymentProcessor: z.string(),
+ metadata: z.record(z.unknown()),
+ }),
+});
+
export const saleEventSchemaTB = clickEventSchemaTB
.omit({ timestamp: true })
.and(