The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
simplify getCustomerEvents
Steven Tey165a73068586
diff --git a/apps/web/app/api/customers/[id]/activity/route.ts b/apps/web/app/api/customers/[id]/activity/route.tsindex d6b93feafd..0188fdb429 100644--- a/apps/web/app/api/customers/[id]/activity/route.ts+++ b/apps/web/app/api/customers/[id]/activity/route.ts@@ -30,15 +30,9 @@ export const GET = withWorkspace(async ({ workspace, params, session }) => {}let [events, link] = await Promise.all([- getCustomerEvents(- {- customerId: customer.id,- },- {- sortOrder: "desc",- interval: "1y",- },- ),+ getCustomerEvents({+ customerId: customer.id,+ }),prisma.link.findUniqueOrThrow({where: {diff --git a/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/activity/route.ts b/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/activity/route.tsindex 27e1086095..0f6441ffa1 100644--- a/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/activity/route.ts+++ b/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/activity/route.ts@@ -42,16 +42,10 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {);}- const events = await getCustomerEvents(- {- customerId: customer.id,- linkIds: links.map((link) => link.id),- },- {- sortOrder: "desc",- interval: "1y",- },- );+ const events = await getCustomerEvents({+ customerId: customer.id,+ linkIds: links.map((link) => link.id),+ });// get the first partner link that this customer interacted withconst firstLinkId = events[0].link_id;diff --git a/apps/web/lib/analytics/get-customer-events.ts b/apps/web/lib/analytics/get-customer-events.tsindex b7dc06f49f..3ac23ddbb0 100644--- a/apps/web/lib/analytics/get-customer-events.ts+++ b/apps/web/lib/analytics/get-customer-events.ts@@ -10,31 +10,14 @@ import {} from "../zod/schemas/clicks";import { leadEventResponseSchema } from "../zod/schemas/leads";import { saleEventResponseSchema } from "../zod/schemas/sales";-import { EventsFilters } from "./types";-import { getStartEndDates } from "./utils/get-start-end-dates";--export const getCustomerEvents = async (- {- customerId,- linkIds,- }: {- customerId: string;- linkIds?: string[];- },- params: Pick<- EventsFilters,- "sortOrder" | "start" | "end" | "dataAvailableFrom" | "interval"- >,-) => {- let { sortOrder, start, end, dataAvailableFrom, interval } = params;-- const { startDate, endDate } = getStartEndDates({- interval,- start,- end,- dataAvailableFrom,- });+export const getCustomerEvents = async ({+ customerId,+ linkIds,+}: {+ customerId: string;+ linkIds?: string[];+}) => {const pipe = tb.buildPipe({pipe: "v2_customer_events",parameters: z.any(), // TODO@@ -42,12 +25,8 @@ export const getCustomerEvents = async (});const response = await pipe({- ...params,customerId,...(linkIds ? { linkIds } : {}),- order: sortOrder,- start: startDate.toISOString().replace("T", " ").replace("Z", ""),- end: endDate.toISOString().replace("T", " ").replace("Z", ""),});const linksMap = await getLinksMap(response.data.map((d) => d.link_id));diff --git a/packages/tinybird/pipes/v2_customer_events.pipe b/packages/tinybird/pipes/v2_customer_events.pipeindex 50e6d46907..4e706f5003 100644--- a/packages/tinybird/pipes/v2_customer_events.pipe+++ b/packages/tinybird/pipes/v2_customer_events.pipe@@ -46,8 +46,6 @@ SQL >)}}{% if defined(linkIds) %} AND link_id IN ({{ Array(linkIds, 'link_id') }}) {% end %}- AND timestamp >= {{ DateTime(start, '2024-01-01 00:00:00') }}- AND timestamp < {{ DateTime(end, '2025-06-30 00:00:00') }}ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}LIMIT {{ Int32(limit, 100) }}@@ -133,8 +131,6 @@ SQL >)}}{% if defined(linkIds) %} AND link_id IN ({{ Array(linkIds, 'link_id') }}) {% end %}- AND timestamp >= {{ DateTime(start, '2025-01-01 00:00:00') }}- AND timestamp < {{ DateTime(end, '2025-06-30 00:00:00') }}LIMIT {{ Int32(limit, 100) }}