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.

refactor lead tracking

Kiran K43b6d158a84b
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 39f38bef1c..8edc46a6d3 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -30,27 +30,31 @@ export const POST = withSessionEdge(async ({ req, searchParams }) => {
return;
}
+ const timestamp = new Date(Date.now()).toISOString();
+ const customerInfoPresent =
+ Boolean(customerName) ||
+ Boolean(customerEmail) ||
+ Boolean(customerAvatar);
+
await Promise.all([
recordLead({
...clickEvent.data[0],
- timestamp: new Date(Date.now()).toISOString(),
+ timestamp,
event_name: eventName,
event_id: nanoid(16),
customer_id: customerId,
metadata,
}),
- ...(customerName || customerEmail || customerAvatar
- ? [
- recordCustomer({
- timestamp: new Date(Date.now()).toISOString(),
- customer_id: customerId,
- name: customerName,
- email: customerEmail,
- avatar: customerAvatar,
- workspace_id: workspaceId,
- }),
- ]
- : []),
+
+ customerInfoPresent &&
+ recordCustomer({
+ timestamp,
+ customer_id: customerId,
+ name: customerName,
+ email: customerEmail,
+ avatar: customerAvatar,
+ workspace_id: workspaceId,
+ }),
]);
})(),
);
diff --git a/apps/web/lib/auth/session-edge.ts b/apps/web/lib/auth/session-edge.ts
index 7e2b6142fd..30cb74c9c5 100644
--- a/apps/web/lib/auth/session-edge.ts
+++ b/apps/web/lib/auth/session-edge.ts
@@ -73,6 +73,9 @@ export const withSessionEdge =
},
};
+ // TODO:
+ // Extract workspaceId and validate it
+
// Fetch search params
const searchParams = getSearchParams(req.url);
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 058b5486b1..d87781ec71 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -11,8 +11,8 @@ export const customersMetadataSchema = z.object({
timestamp: z.string(),
customer_id: z.string(),
name: z.string().nullish().default(""),
- email: z.string().email().nullish().default(""),
- avatar: z.string().url().nullish().default(""),
+ email: z.string().nullish().default(""),
+ avatar: z.string().nullish().default(""),
workspace_id: z.string(),
deleted: z.boolean().default(false),
});
diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.ts
index 69e2935131..ae58b69883 100644
--- a/apps/web/lib/zod/schemas/leads.ts
+++ b/apps/web/lib/zod/schemas/leads.ts
@@ -20,7 +20,7 @@ export const trackLeadRequestSchema = z.object({
customerName: z
.string()
.max(100)
- .optional()
+ .nullish()
.describe(
"This is the name of the customer in the client's app. This is used to track the customer's journey.",
),
@@ -28,14 +28,14 @@ export const trackLeadRequestSchema = z.object({
.string()
.email()
.max(100)
- .optional()
+ .nullish()
.describe(
"This is the email of the customer in the client's app. This is used to track the customer's journey.",
),
customerAvatar: z
.string()
.max(100)
- .optional()
+ .nullish()
.describe(
"This is the avatar of the customer in the client's app. This is used to track the customer's journey.",
),
diff --git a/packages/tinybird/datasources/dub_customers_metadata.datasource b/packages/tinybird/datasources/dub_customers_metadata.datasource
index ca7932058a..d333410fad 100644
--- a/packages/tinybird/datasources/dub_customers_metadata.datasource
+++ b/packages/tinybird/datasources/dub_customers_metadata.datasource
@@ -1,11 +1,11 @@
SCHEMA >
- `timestamp` DateTime `json:$.timestamp`,
- `customer_id` String `json:$.customer_id`,
- `name` String `json:$.name`,
- `email` String `json:$.email`,
- `avatar` String `json:$.avatar`,
- `workspace_id` String `json:$.workspace_id`,
- `deleted` UInt8 `json:$.deleted`
+ `timestamp` DateTime `json:$.timestamp`,
+ `customer_id` String `json:$.customer_id`,
+ `name` String `json:$.name`,
+ `email` String `json:$.email`,
+ `avatar` String `json:$.avatar`,
+ `workspace_id` String `json:$.workspace_id`,
+ `deleted` UInt8 `json:$.deleted`
ENGINE "MergeTree"
ENGINE_SORTING_KEY "timestamp, customer_id, workspace_id, deleted"
\ No newline at end of file