The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improve leadCache handling
Steven Tey3eb75c716feb
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex 4a95291871..b5e4102bea 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -39,6 +39,7 @@ export const POST = withWorkspace(mode = "async", // Default to async mode if not specified} = trackLeadRequestSchema.parse(await parseRequestBody(req));+ const stringifiedEventName = eventName.toLowerCase().replace(" ", "-");const customerExternalId = externalId || customerId;const finalCustomerName =customerName || customerEmail || generateRandomName();@@ -52,7 +53,7 @@ export const POST = withWorkspace(// deduplicate lead events – only record 1 unique event for the same customer and event nameconst ok = await redis.set(- `trackLead:${workspace.id}:${customerExternalId}:${eventName.toLowerCase().replace(" ", "-")}`,+ `trackLead:${workspace.id}:${customerExternalId}:${stringifiedEventName}`,{timestamp: Date.now(),clickId,@@ -77,7 +78,7 @@ export const POST = withWorkspace(}if (!clickData) {- clickData = await redis.get<ClickData>(`click:${clickId}`);+ clickData = await redis.get<ClickData>(`clickCache:${clickId}`);if (clickData) {clickData = {@@ -159,7 +160,7 @@ export const POST = withWorkspace(// Cache the latest lead event for 5 minutes because the ingested event is not available immediately on Tinybirdredis.set(- `latestLeadEvent:${customer.id}`,+ `leadCache:${customer.id}:${stringifiedEventName}`,Array.isArray(leadEventPayload)? leadEventPayload[0]: leadEventPayload,diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.tsindex 12cc70f46e..d4006cac9a 100644--- a/apps/web/app/api/track/sale/route.ts+++ b/apps/web/app/api/track/sale/route.ts@@ -90,15 +90,27 @@ export const POST = withWorkspace(let leadEventData: LeadEvent | null = null;if (!leadEvent || leadEvent.data.length === 0) {+ let cachedLeadEvent: LeadEvent | null = null;// Check cache to see if the lead event exists- const cachedLeadEvent = await redis.get<LeadEvent>(- `latestLeadEvent:${customer.id}`,- );+ // if leadEventName is provided, we only check for that specific event+ // otherwise, we check for all cached lead events for that customer++ if (leadEventName) {+ cachedLeadEvent = await redis.get<LeadEvent>(+ `leadCache:${customer.id}:${leadEventName.toLowerCase().replace(" ", "-")}`,+ );+ } else {+ const cachedLeadEventKeys = await redis.scan(0, {+ match: `leadCache:${customer.id}:*`,+ });++ cachedLeadEvent = await redis.get<LeadEvent>(cachedLeadEventKeys[1][0]);+ }if (!cachedLeadEvent) {throw new DubApiError({code: "not_found",- message: `Lead event not found for externalId: ${customerExternalId}`,+ message: `Lead event not found for externalId: ${customerExternalId} and eventName: ${leadEventName}`,});}diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.tsindex fa34b900be..d66abccd46 100644--- a/apps/web/lib/tinybird/record-click.ts+++ b/apps/web/lib/tinybird/record-click.ts@@ -157,7 +157,7 @@ export async function recordClick({// cache the click data for 5 mins// we're doing this because ingested click events are not available immediately in TinybirdtrackConversion &&- redis.set(`click:${clickId}`, clickData, {+ redis.set(`clickCache:${clickId}`, clickData, {ex: 60 * 5,}),