The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
improve track open to receive `deepLink` instead of domain+key
Steven Tey7d5108322abc
diff --git a/apps/web/app/(ee)/api/track/open/route.ts b/apps/web/app/(ee)/api/track/open/route.tsindex 6319f3969b..c235424b77 100644--- a/apps/web/app/(ee)/api/track/open/route.ts+++ b/apps/web/app/(ee)/api/track/open/route.ts@@ -18,9 +18,24 @@ const CORS_HEADERS = {"Access-Control-Allow-Headers": "Content-Type, Authorization",};+// Utility function to parse deep link URL and extract domain and key+const parseDeepLink = (deepLink: string): { domain: string; key: string } => {+ try {+ const url = new URL(deepLink);+ const domain = url.hostname.replace(/^www\./, "").toLowerCase();+ const key = url.pathname.slice(1) || "_root"; // Remove leading slash, default to _root if empty++ return { domain, key };+ } catch (error) {+ throw new DubApiError({+ code: "bad_request",+ message: "Invalid deep link URL format",+ });+ }+};+const trackOpenRequestSchema = z.object({- domain: z.string().trim(),- key: z.string().trim(),+ deepLink: z.string().trim().url("Invalid deep link URL format"),});const trackOpenResponseSchema = z.object({@@ -36,10 +51,12 @@ const trackOpenResponseSchema = z.object({// POST /api/track/open – Track an open event for deep linkexport const POST = withAxiom(async (req: AxiomRequest) => {try {- const { domain, key } = trackOpenRequestSchema.parse(+ const { deepLink } = trackOpenRequestSchema.parse(await parseRequestBody(req),);+ const { domain, key } = parseDeepLink(deepLink);+const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;let [cachedClickId, cachedLink] = await redis.mget<@@ -62,7 +79,7 @@ export const POST = withAxiom(async (req: AxiomRequest) => {if (!link) {throw new DubApiError({code: "not_found",- message: `Link not found for the short link https://${domain}/${key}`,+ message: `Deep link not found: ${deepLink}`,});}@@ -74,7 +91,7 @@ export const POST = withAxiom(async (req: AxiomRequest) => {if (!cachedLink.projectId) {throw new DubApiError({code: "not_found",- message: "Link does not belong to a workspace.",+ message: "Deep link does not belong to a workspace.",});}diff --git a/apps/web/tests/tracks/track-open.test.ts b/apps/web/tests/tracks/track-open.test.tsindex 586f1a839c..a360ff4e20 100644--- a/apps/web/tests/tracks/track-open.test.ts+++ b/apps/web/tests/tracks/track-open.test.ts@@ -29,8 +29,7 @@ describe("POST /track/open", async () => {path: "/track/open",headers: E2E_TRACK_CLICK_HEADERS,body: {- domain: E2E_LINK.domain,- key: E2E_LINK.key,+ deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,},});@@ -53,8 +52,7 @@ describe("POST /track/open", async () => {path: "/track/open",headers: E2E_TRACK_CLICK_HEADERS,body: {- domain: E2E_LINK.domain,- key: E2E_LINK.key,+ deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,},});@@ -66,8 +64,7 @@ describe("POST /track/open", async () => {path: "/track/open",headers: E2E_TRACK_CLICK_HEADERS,body: {- domain: E2E_LINK.domain,- key: "non-existent-key",+ deepLink: `https://${E2E_LINK.domain}/non-existent-key`,},});@@ -75,7 +72,7 @@ describe("POST /track/open", async () => {expect(response.data).toStrictEqual({error: {code: "not_found",- message: `Link not found for the short link https://${E2E_LINK.domain}/non-existent-key`,+ message: `Deep link not found: https://${E2E_LINK.domain}/non-existent-key`,doc_url: "https://dub.co/docs/api-reference/errors#not-found",},});@@ -104,8 +101,7 @@ describe("POST /track/open", async () => {path: "/track/open",headers: E2E_TRACK_CLICK_HEADERS,body: {- domain: E2E_LINK.domain,- key: E2E_LINK.key,+ deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,},});@@ -117,8 +113,7 @@ describe("POST /track/open", async () => {path: "/track/open",headers: E2E_TRACK_CLICK_HEADERS,body: {- domain: E2E_LINK.domain,- key: E2E_LINK.key,+ deepLink: `https://${E2E_LINK.domain}/${E2E_LINK.key}`,},});