The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add linkId / externalId to analytics endpoints
Steven Tey3c9e02acb198
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.tsindex 6ef318609b..360d4c0917 100644--- a/apps/web/lib/auth/workspace.ts+++ b/apps/web/lib/auth/workspace.ts@@ -69,7 +69,6 @@ export const withWorkspace = ({ params }: { params: Record<string, string> | undefined },) => {const searchParams = getSearchParams(req.url);- const { linkId } = params || {};let apiKey: string | undefined = undefined;@@ -88,11 +87,13 @@ export const withWorkspace = (const domain = params?.domain || searchParams.domain;const key = searchParams.key;+ const linkId =+ params?.linkId || searchParams.linkId || searchParams.externalId;let session: Session | undefined;let headers = {};- let id: string | undefined;- let slug: string | undefined;+ let workspaceId: string | undefined;+ let workspaceSlug: string | undefined;const idOrSlug =params?.idOrSlug ||@@ -121,9 +122,9 @@ export const withWorkspace = (}if (idOrSlug.startsWith("ws_")) {- id = idOrSlug.replace("ws_", "");+ workspaceId = idOrSlug.replace("ws_", "");} else {- slug = idOrSlug;+ workspaceSlug = idOrSlug;}if (apiKey) {@@ -198,8 +199,8 @@ export const withWorkspace = (let [workspace, link] = (await Promise.all([prisma.project.findUnique({where: {- id: id || undefined,- slug: slug || undefined,+ id: workspaceId || undefined,+ slug: workspaceSlug || undefined,},include: {users: {@@ -221,8 +222,13 @@ export const withWorkspace = (linkId? prisma.link.findUnique({where: {- ...(linkId.startsWith("ext_")- ? { externalId: linkId.replace("ext_", "") }+ ...(linkId.startsWith("ext_") && workspaceId+ ? {+ projectId_externalId: {+ projectId: workspaceId,+ externalId: linkId.replace("ext_", ""),+ },+ }: { id: linkId }),},})@@ -252,6 +258,19 @@ export const withWorkspace = (});}+ // edge case where linkId is an externalId and workspaceId was not provided (they must've used projectSlug instead)+ // in this case, we need to try fetching the link again+ if (linkId.startsWith("ext_") && !link && !workspaceId) {+ link = (await prisma.link.findUnique({+ where: {+ projectId_externalId: {+ projectId: workspace.id,+ externalId: linkId.replace("ext_", ""),+ },+ },+ })) as LinkProps;+ }+// if domain is defined:// - it's a dub domain and domainChecks is required, check if the user is part of the dub workspace// - it's a custom domain, check if the domain belongs to the workspacediff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.tsindex abf08927af..26554fa91e 100644--- a/apps/web/lib/zod/schemas/analytics.ts+++ b/apps/web/lib/zod/schemas/analytics.ts@@ -6,6 +6,16 @@ import { booleanQuerySchema } from "./misc";export const getAnalyticsQuerySchema = z.object({domain: z.string().optional().describe("The domain of the short link."),key: z.string().optional().describe("The short link slug."),+ linkId: z+ .string()+ .optional()+ .describe("The unique ID of the short link on Dub."),+ externalId: z+ .string()+ .optional()+ .describe(+ "This is the ID of the link in the your database. Must be prefixed with 'ext_' when passed as a query parameter.",+ ),interval: z.enum(intervals).optional()diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex 1b272f08b1..1bd8107bca 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -140,7 +140,7 @@ export const createLinkBodySchema = z.object({.max(255).nullish().describe(- "This is the ID of the link in the your system. If set, it can be used to identify the link in the future.",+ "This is the ID of the link in your database. If set, it can be used to identify the link in the future. Must be prefixed with 'ext_' when passed as a query parameter.",).openapi({ example: "123456" }),prefix: z@@ -263,7 +263,7 @@ export const LinkSchema = z.string().nullable().describe(- "This is the ID of the link in the your system. If set, it can be used to identify the link in the future.",+ "This is the ID of the link in your database. If set, it can be used to identify the link in the future. Must be prefixed with 'ext_' when passed as a query parameter.",),url: z.string().url().describe("The destination URL of the short link."),archived: zdiff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prismaindex 1005eedc69..9ddd8a2019 100644--- a/apps/web/prisma/schema.prisma+++ b/apps/web/prisma/schema.prisma@@ -211,7 +211,7 @@ model Link {expiresAt DateTime? // when the link expires – stored on Redis via ttlexpiredUrl String? @db.LongText // URL to redirect the user to when the link is expiredpassword String? // password to access the link- externalId String? @unique // external ID for the link (e.g. 12345)+ externalId String?proxy Boolean @default(false) // Proxy to use custom OG tags (stored on redis) – if false, will use OG tags from target urltitle String? // OG title for the link (e.g. Dub.co - Open-Source Link Management Infrastructure)@@ -259,6 +259,7 @@ model Link {// TODO: might wanna add indices for utm_source, utm_medium, utm_campaign@@unique([domain, key])+ @@unique([projectId, externalId])@@index(projectId)@@index(domain)@@index(proxy)