Skip to content

Open source CapCut alternative.

TypeScript

feat(site): take more than one answer to where an account found us

David Lef9bc7f5df9f6
diff --git a/site/prisma/Onboarding.prisma b/site/prisma/Onboarding.prisma
index 5ccfca5c..b2d2307a 100644
--- a/site/prisma/Onboarding.prisma
+++ b/site/prisma/Onboarding.prisma
@@ -12,9 +12,8 @@ model UserOnboarding {
// Null until the sequence ends; `skipped` says how it ended.
completedAt DateTime?
skipped Boolean @default(false)
- referralSource String?
- // Free text alongside the choice, for answers worth qualifying.
- referralDetail String?
+ // Every source the account picked; the question takes more than one answer.
+ referralSources String[] @default([])
referralAnsweredAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
diff --git a/site/src/app/api/account/onboarding/route.ts b/site/src/app/api/account/onboarding/route.ts
index f2ae1b07..eab177dc 100644
--- a/site/src/app/api/account/onboarding/route.ts
+++ b/site/src/app/api/account/onboarding/route.ts
@@ -7,6 +7,7 @@ import {
} from "@/lib/donkey-api-auth";
import {
ONBOARDING_VERSION,
+ REFERRAL_SOURCES,
isKnownReferralSource,
} from "@/lib/onboarding/sequence";
import { prisma } from "@/lib/prisma";
@@ -17,7 +18,7 @@ type OnboardingState = {
version: number;
completedAt: string | null;
skipped: boolean;
- referralSource: string | null;
+ referralSources: string[];
};
// An account that has never opened the sequence has no row; it reads as an
@@ -26,7 +27,7 @@ const UNSTARTED: OnboardingState = {
version: ONBOARDING_VERSION,
completedAt: null,
skipped: false,
- referralSource: null,
+ referralSources: [],
};
export const GET = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) => {
@@ -38,13 +39,14 @@ export const GET = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) =>
const updateSchema = z
.object({
- referralSource: z.string().refine(isKnownReferralSource),
- referralDetail: z.string().max(200).optional(),
+ referralSources: z
+ .array(z.string().refine(isKnownReferralSource))
+ .max(REFERRAL_SOURCES.length),
})
.or(z.object({ completed: z.literal(true), skipped: z.boolean() }));
// One write for both things the sequence records: the referral answer when it's
-// picked, and the run's end. Either is the account's first row, so both upsert.
+// given, and the run's end. Either is the account's first row, so both upsert.
export const PUT = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) => {
const parsed = updateSchema.safeParse(await request.json().catch(() => null));
if (!parsed.success) {
@@ -53,10 +55,9 @@ export const PUT = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) =>
const userId = request.donkey.userId;
const data =
- "referralSource" in parsed.data
+ "referralSources" in parsed.data
? {
- referralSource: parsed.data.referralSource,
- referralDetail: parsed.data.referralDetail ?? null,
+ referralSources: [...new Set(parsed.data.referralSources)],
referralAnsweredAt: new Date(),
}
: {
@@ -77,12 +78,12 @@ function toState(row: {
version: number;
completedAt: Date | null;
skipped: boolean;
- referralSource: string | null;
+ referralSources: string[];
}): OnboardingState {
return {
version: row.version,
completedAt: row.completedAt?.toISOString() ?? null,
skipped: row.skipped,
- referralSource: row.referralSource,
+ referralSources: row.referralSources,
};
}
diff --git a/site/src/lib/analytics.ts b/site/src/lib/analytics.ts
index a2cd09c8..34d88c23 100644
--- a/site/src/lib/analytics.ts
+++ b/site/src/lib/analytics.ts
@@ -18,7 +18,7 @@ export type AnalyticsEvents = {
app_install_clicked: { source: "connect_gate_button" | "connect_gate_link" };
// The welcome sequence a new account sees, and its replay from settings.
onboarding_started: { source: OnboardingRun };
- onboarding_referral_selected: { referralSource: ReferralSource };
+ onboarding_referral_selected: { referralSources: ReferralSource[] };
onboarding_completed: { source: OnboardingRun; skipped: boolean; step: number };
// Cut projects home.
project_created: { source: "projects_home" | "sidebar" | "file_import" };
diff --git a/site/src/queries/onboarding.ts b/site/src/queries/onboarding.ts
index dd1fbe6d..c623e819 100644
--- a/site/src/queries/onboarding.ts
+++ b/site/src/queries/onboarding.ts
@@ -11,11 +11,11 @@ export type OnboardingState = {
version: number;
completedAt: string | null;
skipped: boolean;
- referralSource: string | null;
+ referralSources: string[];
};
type OnboardingUpdate =
- | { referralSource: ReferralSource; referralDetail?: string }
+ | { referralSources: ReferralSource[] }
| { completed: true; skipped: boolean };
/** Whether this account still owes us a run of the current sequence. */