diff --git a/.env.example b/.env.example index 5fcb4d0..7fbf195 100644 --- a/.env.example +++ b/.env.example @@ -15,6 +15,7 @@ NEXT_PUBLIC_POSTHOG_KEY= # Set on the Convex deployment (not here): # CLERK_JWT_ISSUER_DOMAIN - the Frontend API URL of your Clerk app, e.g. https://clerk.pseudoeditor.dev # CLERK_WEBHOOK_SECRET - signing secret of the Clerk webhook pointing at /clerk/webhook -# (events: user.created, user.deleted) +# (events: user.created, user.deleted). For production, set it in Vercel's +# Production environment instead: the build copies it to Convex. # POSTHOG_PROJECT_KEY - production deployment only: same key as NEXT_PUBLIC_POSTHOG_KEY, for anonymous -# account counts +# account counts. The production build copies it from NEXT_PUBLIC_POSTHOG_KEY. diff --git a/README.md b/README.md index 8a4a3b0..e9a4455 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,9 @@ the Production environment. In the PostHog project, turn on **Settings > Project cookieless tracking**, or PostHog drops every event the site sends. Keep session replay off, and set event retention to one year or less to match the privacy policy. -Account counts come from Convex, not the browser. Set `POSTHOG_PROJECT_KEY` (the same project key) on the -production Convex deployment only. The Clerk webhook then sends anonymous `account_created` and `account_deleted` +Account counts come from Convex, not the browser. Each production build copies `NEXT_PUBLIC_POSTHOG_KEY` to +the production Convex deployment as `POSTHOG_PROJECT_KEY`, and copies `CLERK_WEBHOOK_SECRET` when it is set in +Vercel, so set both in Vercel's Production environment rather than on Convex. The Clerk webhook then sends anonymous `account_created` and `account_deleted` events, and a daily cron (`convex/crons.ts`) sends `account_stats`: the number of synced accounts and how many saved their workspace in the last 1, 7, and 30 days. None of these events identify an account. diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh index 94ecee8..1c93eff 100644 --- a/scripts/vercel-build.sh +++ b/scripts/vercel-build.sh @@ -19,6 +19,26 @@ if [[ "${VERCEL_ENV:-}" != "production" && "${CONVEX_DEPLOY_KEY}" == prod:* ]]; exit 0 fi +# Copies a Vercel setting into the production Convex deployment. Only an admin deploy key can write +# production Convex variables, and the production build holds one, so Vercel is the single place to +# set them. The value goes in through stdin, so it never appears in the command line or the build log. +sync_convex_env() { + local name=$1 value=$2 + if [[ -z "$value" ]]; then + echo "$name is unset in Vercel; leaving it unchanged on Convex." + return + fi + printf '%s' "$value" | pnpm exec convex env set "$name" >/dev/null + echo "Synced $name to Convex." +} + +if [[ "${VERCEL_ENV:-}" == "production" && "${CONVEX_DEPLOY_KEY}" == prod:* ]]; then + # Account analytics (convex/analytics.ts) use the same public project key as the browser. + sync_convex_env POSTHOG_PROJECT_KEY "${NEXT_PUBLIC_POSTHOG_KEY:-}" + # Verifies Clerk's user.created and user.deleted webhook (convex/http.ts). + sync_convex_env CLERK_WEBHOOK_SECRET "${CLERK_WEBHOOK_SECRET:-}" +fi + pnpm exec convex deploy \ --cmd "pnpm --filter @pseudoeditor/web build" \ --cmd-url-env-var-name NEXT_PUBLIC_CONVEX_URL