The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
chore: Update track route to record conversion events in Tinybird
Kiran K7842565caa54
diff --git a/apps/web/app/api/(campaign)/track/route.ts b/apps/web/app/api/(campaign)/track/route.tsindex a4b5536d38..693870fd6d 100644--- a/apps/web/app/api/(campaign)/track/route.ts+++ b/apps/web/app/api/(campaign)/track/route.ts@@ -10,7 +10,7 @@ export const runtime = "edge";// POST /api/track – Post a conversion eventexport const POST = withSessionEdge(async ({ req }) => {const body = conversionRequestSchema.parse(await parseRequestBody(req));- const { clickId, eventName, eventType, metadata, customerId } = body;+ const { clickId, name, event, metadata, customerKey } = body;waitUntil(async () => {const clickEvent = await getClickEvent({ clickId });@@ -19,13 +19,14 @@ export const POST = withSessionEdge(async ({ req }) => {return;}- console.log("clickEvent", clickEvent.data[0]);+ console.log("body", body);await recordConversion({...clickEvent.data[0],metadata,- event_name: eventName,- event_type: eventType,+ customer_key: customerKey,+ event_name: name,+ event_type: event,timestamp: new Date(Date.now()).toISOString(),});});diff --git a/apps/web/lib/zod/schemas/conversions.ts b/apps/web/lib/zod/schemas/conversions.tsindex d025208e92..ab495e8751 100644--- a/apps/web/lib/zod/schemas/conversions.ts+++ b/apps/web/lib/zod/schemas/conversions.ts@@ -1,10 +1,12 @@import z from "@/lib/zod";-export const eventType = z.enum(["lead", "sale"], {- errorMap: () => ({- message: "Must be either 'lead' or 'sale'",- }),-});+export const eventType = z+ .enum(["lead", "sale"], {+ errorMap: () => ({+ message: "Must be either 'lead' or 'sale'",+ }),+ })+ .default("lead");export const clickEventSchemaTB = z.object({timestamp: z.string(),@@ -38,26 +40,24 @@ export const conversionEventSchemaTB = clickEventSchemaTBz.object({event_name: z.string(),event_type: eventType,- metadata: z.string().nullable(), // TODO: Fix the type- customer_id: z.string().nullish(),+ metadata: z.string(), // TODO: Fix the type+ customer_key: z.string(),}),);-const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);-type Literal = z.infer<typeof literalSchema>;-type Json = Literal | { [key: string]: Json } | Json[];-export const conversionRequestSchema = z.object({clickId: z.string({ required_error: "clickId is required" }),- eventName: z- .string({ required_error: "eventName is required" })- .min(1)- .max(50)- .default(""),- eventType,+ name: z.string().max(50).default(""),+ event: eventType,metadata: z.record(z.unknown()).nullish().transform((val) => (val ? JSON.stringify(val) : "")),- customerId: z.string().max(100).nullish().default(""),+ customerKey: z+ .string()+ .max(100)+ .default("")+ .describe(+ "This is the identifier for the customer in the client's app. This is used to track the customer's journey.",+ ),});diff --git a/packages/tinybird/datasources/dub_conversion_events.datasource b/packages/tinybird/datasources/dub_conversion_events.datasourceindex e2ab3e469f..9fb696f888 100644--- a/packages/tinybird/datasources/dub_conversion_events.datasource+++ b/packages/tinybird/datasources/dub_conversion_events.datasource@@ -6,7 +6,7 @@ SCHEMA >`event_name` LowCardinality(String) `json:$.event_name`,`event_type` LowCardinality(String) `json:$.event_type`,`metadata` String `json:$.metadata`,- `customer_id` String `json:$.customer_id`,+ `customer_key` String `json:$.customer_key`,`country` LowCardinality(String) `json:$.country`,`city` String `json:$.city`,`region` String `json:$.region`,@@ -29,4 +29,4 @@ SCHEMA >ENGINE "MergeTree"ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"-ENGINE_SORTING_KEY "timestamp, link_id, click_id"\ No newline at end of file+ENGINE_SORTING_KEY "timestamp, link_id, click_id, customer_key"\ No newline at end of file