The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add `testsStartedAt`
Tim Wilsonf56c0a7c8a1d
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.tsindex 93e791009e..f0135c9403 100644--- a/apps/web/app/api/links/[linkId]/route.ts+++ b/apps/web/app/api/links/[linkId]/route.ts@@ -172,6 +172,7 @@ export const PATCH = withWorkspace(domain: link.domain,key: link.key,image: link.image,+ testsCompleteAt: link.testsCompleteAt,},updatedLink: processedLink,});diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.tsindex 1349876884..0aadfde2d2 100644--- a/apps/web/app/api/links/bulk/route.ts+++ b/apps/web/app/api/links/bulk/route.ts@@ -394,6 +394,10 @@ export const PATCH = withWorkspace(link.testsCompleteAt instanceof Date? link.testsCompleteAt.toISOString(): link.testsCompleteAt,+ testsStartedAt:+ link.testsStartedAt instanceof Date+ ? link.testsStartedAt.toISOString()+ : link.testsStartedAt,...data,},workspace,diff --git a/apps/web/app/api/links/upsert/route.ts b/apps/web/app/api/links/upsert/route.tsindex 57ec39214b..dfaf4d63e6 100644--- a/apps/web/app/api/links/upsert/route.ts+++ b/apps/web/app/api/links/upsert/route.ts@@ -67,6 +67,10 @@ export const PUT = withWorkspace(link.testsCompleteAt instanceof Date? link.testsCompleteAt.toISOString(): link.testsCompleteAt,+ testsStartedAt:+ link.testsStartedAt instanceof Date+ ? link.testsStartedAt.toISOString()+ : link.testsStartedAt,...body,};@@ -142,6 +146,7 @@ export const PUT = withWorkspace(domain: link.domain,key: link.key,image: link.image,+ testsCompleteAt: link.testsCompleteAt,},updatedLink: processedLink,});diff --git a/apps/web/app/api/partner-profile/programs/[programId]/links/[linkId]/route.ts b/apps/web/app/api/partner-profile/programs/[programId]/links/[linkId]/route.tsindex f707760263..5c4f25f9a4 100644--- a/apps/web/app/api/partner-profile/programs/[programId]/links/[linkId]/route.ts+++ b/apps/web/app/api/partner-profile/programs/[programId]/links/[linkId]/route.ts@@ -67,6 +67,10 @@ export const PATCH = withPartnerProfile(link.testsCompleteAt instanceof Date? link.testsCompleteAt.toISOString(): link.testsCompleteAt,+ testsStartedAt:+ link.testsStartedAt instanceof Date+ ? link.testsStartedAt.toISOString()+ : link.testsStartedAt,// merge in new propskey: key || undefined,@@ -96,6 +100,7 @@ export const PATCH = withPartnerProfile(domain: link.domain,key: link.key,image: link.image,+ testsCompleteAt: link.testsCompleteAt,},updatedLink: processedLink,});diff --git a/apps/web/app/api/partners/links/upsert/route.ts b/apps/web/app/api/partners/links/upsert/route.tsindex 8f0d325353..b65c565231 100644--- a/apps/web/app/api/partners/links/upsert/route.ts+++ b/apps/web/app/api/partners/links/upsert/route.ts@@ -90,6 +90,10 @@ export const PUT = withWorkspace(link.testsCompleteAt instanceof Date? link.testsCompleteAt.toISOString(): link.testsCompleteAt,+ testsStartedAt:+ link.testsStartedAt instanceof Date+ ? link.testsStartedAt.toISOString()+ : link.testsStartedAt,// merge in new props...linkProps,// set default fields@@ -144,6 +148,7 @@ export const PUT = withWorkspace(domain: link.domain,key: link.key,image: link.image,+ testsCompleteAt: link.testsCompleteAt,},updatedLink: processedLink,});diff --git a/apps/web/lib/api/links/complete-tests.ts b/apps/web/lib/api/links/complete-tests.tsindex c9520010d1..24e02ee883 100644--- a/apps/web/lib/api/links/complete-tests.ts+++ b/apps/web/lib/api/links/complete-tests.ts@@ -19,6 +19,7 @@ export async function completeTests(link: Link & { project: Project }) {linkId: link.id,workspaceId: link.projectId,dataAvailableFrom: link.project.createdAt,+ start: link.testsStartedAt ? new Date(link.testsStartedAt) : undefined,end: link.testsCompleteAt,})) as { url: string; leads: number }[];@@ -71,6 +72,10 @@ export async function completeTests(link: Link & { project: Project }) {link.testsCompleteAt instanceof Date? link.testsCompleteAt.toISOString(): link.testsCompleteAt,+ testsStartedAt:+ link.testsStartedAt instanceof Date+ ? link.testsStartedAt.toISOString()+ : link.testsStartedAt,// Update URLurl: winner.url,@@ -106,6 +111,7 @@ export async function completeTests(link: Link & { project: Project }) {domain: link.domain,key: link.key,image: link.image,+ testsCompleteAt: link.testsCompleteAt,},updatedLink: processedLink,});diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.tsindex cd092707ae..990ac51b1c 100644--- a/apps/web/lib/api/links/create-link.ts+++ b/apps/web/lib/api/links/create-link.ts@@ -67,8 +67,9 @@ export async function createLink(link: ProcessedLinkProps) {expiresAt: expiresAt ? new Date(expiresAt) : null,geo: geo || Prisma.JsonNull,- testsCompleteAt: testsCompleteAt ? new Date(testsCompleteAt) : null,tests: tests || Prisma.JsonNull,+ testsCompleteAt: testsCompleteAt ? new Date(testsCompleteAt) : null,+ testsStartedAt: tests && testsCompleteAt ? new Date() : null,// Associate tags by tagNames...(tagNames?.length &&diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex 011f8e818c..b255131c04 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -24,7 +24,12 @@ export async function updateLink({oldLink,updatedLink,}: {- oldLink: { domain: string; key: string; image?: string | null };+ oldLink: {+ domain: string;+ key: string;+ image?: string | null;+ testsCompleteAt?: Date | null;+ };updatedLink: ProcessedLinkProps &Pick<LinkProps, "id" | "clicks" | "lastClicked" | "updatedAt">;}) {@@ -71,6 +76,15 @@ export async function updateLink({key: updatedLink.key,});+ // Update testsStartedAt to now if there are tests to be completed, and the old link had no tests or completed tests+ const testsStartedAt =+ tests &&+ testsCompleteAt &&+ new Date(testsCompleteAt) > new Date() &&+ (!oldLink.testsCompleteAt || new Date(oldLink.testsCompleteAt) < new Date())+ ? new Date()+ : undefined;+const response = await prisma.link.update({where: {id,@@ -96,8 +110,9 @@ export async function updateLink({expiresAt: expiresAt ? new Date(expiresAt) : null,geo: geo || Prisma.JsonNull,- testsCompleteAt: testsCompleteAt ? new Date(testsCompleteAt) : null,tests: tests || Prisma.JsonNull,+ testsCompleteAt: testsCompleteAt ? new Date(testsCompleteAt) : null,+ testsStartedAt,// Associate tags by tagNames...(tagNames &&diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex 3d0145018c..2c66a0187b 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -402,6 +402,10 @@ export const createLinkBodySchema = z.object({"An array of webhook IDs to trigger when the link is clicked. These webhooks will receive click event data.",),tests: LinkTestsSchema.nullish(),+ testsStartedAt: z+ .string()+ .nullish()+ .describe("The date and time when the tests started."),testsCompleteAt: z.string().nullish()@@ -623,6 +627,10 @@ export const LinkSchema = z.nullable().describe("The UTM content of the short link."),tests: LinkTestsSchema.nullish(),+ testsStartedAt: z+ .string()+ .nullish()+ .describe("The date and time when the tests started."),testsCompleteAt: z.string().nullish()@@ -726,7 +734,8 @@ export const linkEventSchema = LinkSchema.extend({updatedAt: z.coerce.date(),lastClicked: z.coerce.date(),expiresAt: z.coerce.date(),- testsCompleteAt: z.coerce.date(),+ testsCompleteAt: z.coerce.date().nullable(),+ testsStartedAt: z.coerce.date().nullable(),// userId can be nulluserId: z.string().nullable(),});diff --git a/apps/web/ui/links/link-tests.tsx b/apps/web/ui/links/link-tests.tsxindex 50cc1ef5f4..5922f1d14e 100644--- a/apps/web/ui/links/link-tests.tsx+++ b/apps/web/ui/links/link-tests.tsx@@ -38,7 +38,15 @@ export const LinkTests = memo(({ link }: { link: ResponseLink }) => {}[]>(Boolean(tests && tests.length) &&- `/api/analytics?event=composite&groupBy=top_urls&linkId=${link.id}&workspaceId=${workspaceId}`,+ `/api/analytics?${new URLSearchParams({+ event: "composite",+ groupBy: "top_urls",+ linkId: link.id,+ workspaceId: workspaceId!,+ ...(link.testsStartedAt && {+ start: new Date(link.testsStartedAt).toISOString(),+ }),+ }).toString()}`,fetcher,{revalidateOnFocus: false,diff --git a/packages/prisma/schema/link.prisma b/packages/prisma/schema/link.prismaindex 837cf26c10..dabb6c6202 100644--- a/packages/prisma/schema/link.prisma+++ b/packages/prisma/schema/link.prisma@@ -35,6 +35,7 @@ model Link {// A/B Testingtests Json? @db.Json // A/B testing data+ testsStartedAt DateTime? // When tests were startedtestsCompleteAt DateTime? // When tests were or will be completed// User who created the link