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 customer activities API

Kiran Kaae9ae443531
diff --git a/apps/web/app/api/customers/[id]/activities/route.ts b/apps/web/app/api/customers/[id]/activities/route.ts
new file mode 100644
index 0000000000..0c1abca6c1
--- /dev/null
+++ b/apps/web/app/api/customers/[id]/activities/route.ts
@@ -0,0 +1,89 @@
+import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
+import { withWorkspace } from "@/lib/auth";
+import { getCustomerEvents } from "@/lib/tinybird/get-customer-events";
+import { CustomerSchema } from "@/lib/zod/schemas/customers";
+import { NextResponse } from "next/server";
+import { z } from "zod";
+
+const CustomerActivitySchema = z.object({
+ timestamp: z.date(),
+ event: z.enum(["click", "lead", "sale"]),
+ event_name: z.string(),
+ metadata: z.union([
+ z.null(),
+ z.object({
+ payment_processor: z.string(),
+ amount: z.string(),
+ }),
+ ]),
+});
+
+const schema = z.object({
+ ltv: z.number(),
+ timeToLead: z.number(),
+ timeToSale: z.number(),
+ customer: CustomerSchema,
+ activities: z.array(CustomerActivitySchema),
+});
+
+// GET /api/customers/[id]/activities - get a customer's activity
+export const GET = withWorkspace(async ({ workspace, params }) => {
+ const { id: customerId } = params;
+
+ const customer = await getCustomerOrThrow({
+ workspaceId: workspace.id,
+ id: customerId,
+ });
+
+ const events = await getCustomerEvents({
+ customerId,
+ });
+
+ const activities = events.data.map((event) => ({
+ ...event,
+ metadata: event.metadata ? JSON.parse(event.metadata) : null,
+ timestamp: new Date(event.timestamp),
+ }));
+
+ // Add click event to activities
+ activities.push({
+ timestamp: customer.clickedAt!,
+ event: "click",
+ event_name: "Link click",
+ metadata: null,
+ });
+
+ // Find the LTV of the customer
+ const ltv = activities.reduce((acc, { event, metadata }) => {
+ if (event === "sale" && metadata) {
+ acc += Number(metadata.amount);
+ }
+
+ return acc;
+ }, 0);
+
+ // Find the time to lead of the customer
+ const timeToLead =
+ customer.clickedAt && customer.leadCreatedAt
+ ? customer.leadCreatedAt.getTime() - customer.clickedAt.getTime()
+ : 0;
+
+ // Find the time to first sale of the customer
+ const firstSale = activities.filter(({ event }) => event === "sale").pop();
+
+ const timeToSale =
+ firstSale && customer.leadCreatedAt
+ ? new Date(firstSale.timestamp).getTime() -
+ customer.leadCreatedAt.getTime()
+ : 0;
+
+ return NextResponse.json(
+ schema.parse({
+ ltv,
+ timeToLead,
+ timeToSale,
+ customer,
+ activities,
+ }),
+ );
+});
diff --git a/apps/web/lib/tinybird/get-customer-events.ts b/apps/web/lib/tinybird/get-customer-events.ts
new file mode 100644
index 0000000000..feb9cab459
--- /dev/null
+++ b/apps/web/lib/tinybird/get-customer-events.ts
@@ -0,0 +1,12 @@
+import z from "../zod";
+import { customerEventsSchemaTB } from "../zod/schemas/customers";
+import { tb } from "./client";
+
+// We only fetch lead and click events from TB
+export const getCustomerEvents = tb.buildPipe({
+ pipe: "get_customer_events",
+ parameters: z.object({
+ customerId: z.string(),
+ }),
+ data: customerEventsSchemaTB,
+});
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 0b5a840bf3..5cc4163294 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -75,6 +75,7 @@ export const trackCustomerResponseSchema = z.object({
});
// simple schema returned by /events API
+// TODO: Remove this schema if it is not used
export const customerSchema = z.object({
id: z.string(),
name: z.string(),
@@ -95,3 +96,10 @@ export const customersQuerySchema = z
.describe("IDs of customers to filter by."),
})
.merge(getPaginationQuerySchema({ pageSize: CUSTOMERS_MAX_PAGE_SIZE }));
+
+export const customerEventsSchemaTB = z.object({
+ timestamp: z.string(),
+ event: z.string(),
+ event_name: z.string(),
+ metadata: z.string().default(""),
+});