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 logging for conversion events in lead and sale tracking

Kiran K82cc26f3e6a6
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 26e143c81e..072d0ec374 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -6,6 +6,7 @@ import { withWorkspace } from "@/lib/auth";
import { generateRandomName } from "@/lib/names";
import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
import { getClickEvent, recordLead, recordLeadSync } from "@/lib/tinybird";
+import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformLeadEventData } from "@/lib/webhook/transform";
@@ -26,6 +27,8 @@ type ClickData = z.infer<typeof clickEventSchemaTB>;
// POST /api/track/lead – Track a lead conversion event
export const POST = withWorkspace(
async ({ req, workspace }) => {
+ const body = await parseRequestBody(req);
+
const {
clickId,
eventName,
@@ -37,7 +40,7 @@ export const POST = withWorkspace(
customerAvatar,
metadata,
mode = "async", // Default to async mode if not specified
- } = trackLeadRequestSchema.parse(await parseRequestBody(req));
+ } = trackLeadRequestSchema.parse(body);
const customerExternalId = externalId || customerId;
const finalCustomerName =
@@ -206,6 +209,13 @@ export const POST = withWorkspace(
},
},
}),
+
+ logConversionEvent({
+ workspace_slug: workspace.slug,
+ link_id: clickData.link_id,
+ path: "/track/lead",
+ body: JSON.stringify(body),
+ }),
]);
if (link.programId && link.partnerId) {
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index c7b9195252..0d84e0e08e 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -8,6 +8,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
+import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformSaleEventData } from "@/lib/webhook/transform";
@@ -29,6 +30,8 @@ type LeadEvent = z.infer<typeof leadEventSchemaTB>;
// POST /api/track/sale – Track a sale conversion event
export const POST = withWorkspace(
async ({ req, workspace }) => {
+ const body = await parseRequestBody(req);
+
let {
externalId,
customerId, // deprecated
@@ -39,7 +42,7 @@ export const POST = withWorkspace(
metadata,
eventName,
leadEventName,
- } = trackSaleRequestSchema.parse(await parseRequestBody(req));
+ } = trackSaleRequestSchema.parse(body);
if (invoiceId) {
// Skip if invoice id is already processed
@@ -77,6 +80,15 @@ export const POST = withWorkspace(
});
if (!customer) {
+ waitUntil(
+ logConversionEvent({
+ workspace_slug: workspace.slug,
+ path: "/track/sale",
+ body: JSON.stringify(body),
+ error: `Customer not found for externalId: ${customerExternalId}`,
+ }),
+ );
+
return NextResponse.json({
eventName,
customer: null,
@@ -172,6 +184,13 @@ export const POST = withWorkspace(
},
},
}),
+
+ logConversionEvent({
+ workspace_slug: workspace.slug,
+ link_id: clickData.link_id,
+ path: "/track/sale",
+ body: JSON.stringify(body),
+ }),
]);
// for program links
diff --git a/apps/web/lib/tinybird/log-conversion-events.ts b/apps/web/lib/tinybird/log-conversion-events.ts
new file mode 100644
index 0000000000..ae1108502c
--- /dev/null
+++ b/apps/web/lib/tinybird/log-conversion-events.ts
@@ -0,0 +1,16 @@
+import { z } from "zod";
+import { tb } from "./client";
+
+const schema = z.object({
+ workspace_slug: z.string(),
+ link_id: z.string().default(""),
+ path: z.string(),
+ body: z.string().default(""),
+ error: z.string().default(""),
+});
+
+// Log the conversion events for debugging purposes
+export const logConversionEvent = tb.buildIngestEndpoint({
+ datasource: "dub_conversion_events_log",
+ event: schema,
+});
diff --git a/packages/tinybird/datasources/dub_conversion_events_log.datasource b/packages/tinybird/datasources/dub_conversion_events_log.datasource
new file mode 100644
index 0000000000..3be7a91c34
--- /dev/null
+++ b/packages/tinybird/datasources/dub_conversion_events_log.datasource
@@ -0,0 +1,14 @@
+# A datasource for logging conversion events for debugging purposes
+
+SCHEMA >
+ `timestamp` DateTime64(3) `json:$.timestamp` DEFAULT now(),
+ `workspace_slug` String `json:$.workspace_slug`,
+ `link_id` String `json:$.link_id`,
+ `path` String `json:$.path`,
+ `body` String `json:$.body`,
+ `error` String `json:$.error`
+
+ENGINE "MergeTree"
+ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"
+ENGINE_SORTING_KEY "timestamp, workspace_slug"
+ENGINE_TTL "toDateTime(timestamp) + toIntervalDay(90)"
\ No newline at end of file