d4f82cafc4
Recapt shuts down in four days, taking product analytics and session replay with it. This adds PostHog Cloud EU alongside it; the Recapt removal follows separately so events can be confirmed landing first. Wiring choices that are not the tutorial defaults: - Same-origin reverse proxy (/rl -> eu.i.posthog.com) instead of adding PostHog hosts to the CSP. connect-src 'self' and script-src 'self' already cover it, tracking blockers have no third-party host to match, and the Recapt allowlist entries in next.config.ts get replaced by nothing at all when they go. Needs skipTrailingSlashRedirect, since PostHog sends trailing-slash API requests; verified that trailing-slash URLs on normal routes still resolve 200 rather than 404. - /rl is excluded from the proxy.ts matcher. Middleware runs BEFORE next.config rewrites, so without this updateSession() treats an ingestion POST as an unknown protected path and 307s it to /login. Verified with a control: /zz/flags/ -> 307 /login, /rl/flags/ -> 200 from PostHog. This fails silently otherwise, because asset loads keep working through the rewrite while no events arrive. - persistence: 'memory' so nothing is written to the device and no cookie-consent banner is required. Everything post-login is unaffected: AnalyticsIdentify re-identifies on each dashboard load. - session_recording.maskTextSelector: '*'. PostHog masks inputs but not text by default, and this app renders org numbers (which for an enskild firma ARE the owner's personnummer), customer names and balances as ordinary text. Replays show where a user gets stuck, never what their books say. buildGroupProperties() also refuses to send org_number at all, with a test pinning it. - Error tracking registers through the existing lib/observability sink rather than bypassing it, so every error-level createLogger() line is captured already redacted. instrumentation.ts onRequestError covers what escapes uncaught. Analytics is hosted-only: isAnalyticsEnabled() short-circuits on NEXT_PUBLIC_SELF_HOSTED and no Docker sentinel is added, so self-hosted runs with zero third-party runtime code. Recapt got that outcome only by accident, via a missing sentinel; here it is explicit and tested. vitest.config.ts aliases 'server-only' to a stub: it is a build-time guard whose real entry point always throws, which broke 48 test files the moment a server-only module entered the graph. request-context.ts was already carrying the same latent trap. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
79 lines
2.9 KiB
TypeScript
79 lines
2.9 KiB
TypeScript
import type { AccountingFramework, CompanyRole, EntityType } from '@/types'
|
|
|
|
/**
|
|
* Property builders for PostHog identify + group calls.
|
|
*
|
|
* Pure functions on purpose: the repo's Vitest runs in a `node` environment
|
|
* and `vitest.config.ts` only matches `*.test.ts`, so component JSX is
|
|
* untestable by convention. Keeping the property shaping here means the part
|
|
* that can actually be wrong (what we send about a person and a company) is
|
|
* covered by unit tests, while the mounting component stays trivial.
|
|
*
|
|
* Two rules encoded here, both from PostHog's framework guidance:
|
|
*
|
|
* 1. PII belongs in identify() PERSON properties, never in capture() event
|
|
* properties. Only `buildPersonProperties` carries email/name.
|
|
* 2. `org_number` is deliberately NEVER sent. For an enskild firma the
|
|
* organisationsnummer IS the owner's personnummer, so it is not a company
|
|
* identifier at all: it is a national identity number for a natural
|
|
* person. `company_id` is the group key and is already unique.
|
|
*/
|
|
|
|
export interface AnalyticsUserInput {
|
|
userId: string
|
|
email?: string | null
|
|
fullName?: string | null
|
|
role?: CompanyRole | null
|
|
}
|
|
|
|
export interface AnalyticsCompanyInput {
|
|
id: string
|
|
name: string
|
|
entityType?: EntityType | null
|
|
accountingFramework?: AccountingFramework | null
|
|
paysSalaries?: boolean | null
|
|
trialEndsAt?: string | null
|
|
capabilities?: readonly string[]
|
|
}
|
|
|
|
/** Drop null/undefined so PostHog person properties don't fill with empties. */
|
|
function compact(input: Record<string, unknown>): Record<string, unknown> {
|
|
return Object.fromEntries(
|
|
Object.entries(input).filter(([, value]) => value !== null && value !== undefined)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Person properties for `posthog.identify(userId, props)`.
|
|
*
|
|
* Matches what the privacy policy discloses is transferred: user id, email
|
|
* address and company name. Do not widen this without updating
|
|
* app/(public)/privacy/page.tsx and .compliance/ropa.yaml.
|
|
*/
|
|
export function buildPersonProperties(user: AnalyticsUserInput): Record<string, unknown> {
|
|
return compact({
|
|
email: user.email,
|
|
name: user.fullName,
|
|
role: user.role,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Group properties for `posthog.group('company', companyId, props)`.
|
|
*
|
|
* Company-shaped facts only: anything that varies per user (role) belongs on
|
|
* the person, not the group. No org_number, see the file header.
|
|
*/
|
|
export function buildGroupProperties(company: AnalyticsCompanyInput): Record<string, unknown> {
|
|
return compact({
|
|
name: company.name,
|
|
entity_type: company.entityType,
|
|
accounting_framework: company.accountingFramework,
|
|
pays_salaries: company.paysSalaries,
|
|
trial_ends_at: company.trialEndsAt,
|
|
// Sorted so the same entitlement set doesn't look like a change every
|
|
// time the underlying query returns a different order.
|
|
capabilities: company.capabilities ? [...company.capabilities].sort() : undefined,
|
|
})
|
|
}
|