Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eventQuantity support #2050

Merged
merged 3 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions apps/web/app/api/track/lead/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const POST = withWorkspaceEdge(
const {
clickId,
eventName,
eventQuantity,
externalId,
customerId, // deprecated (but we'll support it for backwards compatibility)
customerName,
Expand Down Expand Up @@ -106,15 +107,25 @@ export const POST = withWorkspaceEdge(
});

const eventId = nanoid(16);
const leadEventPayload = {
...clickData,
event_id: eventId,
event_name: eventName,
customer_id: customer.id,
metadata: metadata ? JSON.stringify(metadata) : "",
};

const [_lead, link, _project] = await Promise.all([
recordLead({
...clickData,
event_id: eventId,
event_name: eventName,
customer_id: customer.id,
metadata: metadata ? JSON.stringify(metadata) : "",
}),
recordLead(
eventQuantity
? Array(eventQuantity)
.fill(null)
.map(() => ({
...leadEventPayload,
event_id: nanoid(16),
}))
: leadEventPayload,
),

// update link leads count
prismaEdge.link.update({
Expand All @@ -123,7 +134,7 @@ export const POST = withWorkspaceEdge(
},
data: {
leads: {
increment: 1,
increment: eventQuantity ?? 1,
},
},
include: includeTags,
Expand All @@ -136,7 +147,7 @@ export const POST = withWorkspaceEdge(
},
data: {
usage: {
increment: 1,
increment: eventQuantity ?? 1,
},
},
}),
Expand All @@ -160,8 +171,10 @@ export const POST = withWorkspaceEdge(
customerId: customer.id,
type: "lead",
amount: 0,
quantity: 1,
earnings: reward.amount,
quantity: eventQuantity ?? 1,
earnings: eventQuantity
? reward.amount * eventQuantity
: reward.amount,
},
});
}
Expand Down
10 changes: 8 additions & 2 deletions apps/web/lib/zod/schemas/leads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ export const trackLeadRequestSchema = z.object({
.trim()
.min(1, "clickId is required")
.describe(
"The ID of the click in th Dub. You can read this value from `dub_id` cookie.",
"The ID of the click in Dub. You can read this value from `dub_id` cookie.",
),
eventName: z
.string({ required_error: "eventName is required" })
.trim()
.min(1, "eventName is required")
.max(255)
.describe("The name of the event to track.")
.describe("The name of the lead event to track.")
.openapi({ example: "Sign up" }),
eventQuantity: z
.number()
.nullish()
.describe(
"The numerical value associated with this lead event (e.g., number of provisioned seats in a free trial). If defined as N, the lead event will be tracked N times.",
),
externalId: z
.string()
.trim()
Expand Down
2 changes: 1 addition & 1 deletion apps/web/lib/zod/schemas/sales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const trackSaleRequestSchema = z.object({
.nullish()
.default(null)
.describe(
"The name of the lead event that occurred before the sale (case-sensitive).",
"The name of the lead event that occurred before the sale (case-sensitive). This is used to associate the sale event with a particular lead event (instead of the latest lead event, which is the default behavior).",
)
.openapi({ example: "Cloned template 1481267" }),
});
Expand Down
Loading