248d98bd7e
* feat(analytics): remove Recapt, PostHog is now the only analytics
Recapt shuts down in days. Everything it did is covered by the PostHog
integration in the previous commit, so the SDK, its five modules and its
CSP hosts come out.
Deleted: RecaptLoader, RecaptHideWidget, RecaptIdentify, lib/recapt.ts,
types/recapt.d.ts. Unmounted from app/layout.tsx (the <script> in <head>
and the widget-hider) and from app/(dashboard)/layout.tsx. Both logout
handlers already call resetAnalyticsIdentity() and now only that.
The CSP gets strictly narrower: connect-src loses api.recapt.app and
cdn.recapt.app, script-src loses cdn.recapt.app, and nothing is added in
their place, because PostHog runs through the same-origin /rl rewrite.
Verified against the built routes-manifest.
Behaviour change worth calling out: lib/support/submit-feedback.ts is now
single-channel. Recapt used to accept the message through its own SDK, so
a failing /api/support/contact still reported success to the user. Email
is now the only delivery path and its failure is visible. That is the
right outcome, silently "succeeding" while the message reached nobody was
worse, and the Resend path is solid. A non-blocking
posthog.capture('support_feedback_submitted') keeps the useful half of
the old dual-channel behaviour by putting the submission on the user's
timeline next to the session replay; it carries no message body, since
free text is user content and would be PII in an event property. The six
Recapt-specific test cases are replaced with the email-only contract plus
coverage of the breadcrumb, the self-hosted skip, and a throwing SDK not
breaking delivery.
Compliance, which Recapt never had: the privacy page sub-processor row is
replaced (not just deleted) with an accurate PostHog row, and .compliance/
ropa.yaml gains a product.analytics activity. The old row also claimed
Recapt loaded "endast for inloggade anvandare", which was never true,
RecaptLoader sat in the root <head> on every page including logged-out
ones. The new row describes what actually happens.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(analytics): purge Recapt storage left on users' devices
Removing the Recapt <script> stops it writing anything new, but every
browser that already loaded the app keeps what it persisted. Observed on
production after #1237: localStorage still holds
`__recapt_record_engine`, and after this PR nothing would ever remove it,
because the helper that used to sweep on logout (lib/recapt.ts
clearRecaptIdentity) is deleted along with the SDK.
Inert data, but it is third-party storage from a processor the privacy
page now says we no longer use, and the whole point of the PostHog
config is that nothing is stored on the device. So clear it.
Matching is by substring rather than prefix on purpose: the old sweep
tested key.startsWith('recapt'), which never actually matched the real
key, since `__recapt_record_engine` starts with underscores. A test pins
that. The app's own keys (Accounted:chat-sidebar-collapsed,
gnubok.inbox.onboarding.dismissed) contain neither marker.
Runs unconditionally from instrumentation-client.ts, before the
analytics gate, so a browser gets cleaned even on a build where PostHog
is switched off. Iterates backwards because removeItem() re-indexes the
store and a forward loop would skip entries; both covered by tests, along
with private-mode throws and the server no-op.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
81 lines
3.5 KiB
TypeScript
81 lines
3.5 KiB
TypeScript
import posthog from 'posthog-js'
|
|
import { isAnalyticsEnabled, warnIfAnalyticsMisconfigured } from '@/lib/analytics/enabled'
|
|
import { purgeLegacyAnalyticsStorage } from '@/lib/analytics/purge-legacy-storage'
|
|
|
|
// Clear anything Recapt left on the device. Runs unconditionally, BEFORE the
|
|
// analytics gate: a browser carrying `__recapt_record_engine` must get cleaned
|
|
// up even on a build where PostHog itself is switched off.
|
|
purgeLegacyAnalyticsStorage()
|
|
|
|
/**
|
|
* Hostnames that get the X-POSTHOG-DISTINCT-ID / X-POSTHOG-SESSION-ID headers,
|
|
* which is what lets a server error captured in instrumentation.ts link back
|
|
* to this user's session replay.
|
|
*
|
|
* Deliberately our own origin only. Listing a Supabase or third-party host
|
|
* here would leak PostHog identifiers to them. PostHog matches on hostname
|
|
* alone, so no protocol and no port ('localhost', never 'localhost:3000').
|
|
*/
|
|
function tracingHosts(): string[] {
|
|
const hosts = ['localhost', '127.0.0.1']
|
|
const appUrl = process.env.NEXT_PUBLIC_APP_URL
|
|
if (appUrl) {
|
|
try {
|
|
hosts.push(new URL(appUrl).hostname)
|
|
} catch {
|
|
// Malformed NEXT_PUBLIC_APP_URL: skip rather than break init.
|
|
}
|
|
}
|
|
return hosts
|
|
}
|
|
|
|
/**
|
|
* Client-side PostHog initialisation.
|
|
*
|
|
* This file is the ONLY place posthog.init() is called. Next.js 15.3+ runs
|
|
* `instrumentation-client` before hydration, which is what PostHog's own
|
|
* Next.js guidance requires; deliberately NOT combined with a
|
|
* <PostHogProvider> wrapper, which their example calls out as a mistake.
|
|
*
|
|
* Three choices here are deliberate and worth not "fixing":
|
|
*
|
|
* 1. `api_host: '/rl'` routes every request through the same-origin rewrite
|
|
* in next.config.ts. That keeps PostHog first-party, so the strict CSP
|
|
* needs no third-party hosts at all (`connect-src 'self'` already covers
|
|
* it) and ad blockers have nothing to match on. The path must stay in the
|
|
* proxy.ts matcher exclusion or middleware bounces it to /login.
|
|
*
|
|
* 2. `persistence: 'memory'` stores nothing on the device. That is what lets
|
|
* us run analytics without a cookie-consent banner. The cost is that an
|
|
* anonymous visitor's identity does not survive a hard reload; everything
|
|
* post-login is unaffected because AnalyticsIdentify re-identifies on
|
|
* every dashboard load. Note that surveys still write their own
|
|
* `seenSurvey_*` flags straight to localStorage, bypassing this setting:
|
|
* that is functional UI state ("don't ask again"), not tracking.
|
|
*
|
|
* 3. `maskTextSelector: '*'` (PostHog's documented way to mask ALL text) on
|
|
* top of the default `maskAllInputs`. This is an accounting app: org
|
|
* numbers (which for an enskild firma ARE the owner's personnummer),
|
|
* customer names, balances and invoice amounts are rendered as ordinary
|
|
* text, and PostHog masks inputs but NOT text by default. Replays are for
|
|
* seeing WHERE a user gets stuck, never WHAT their books say.
|
|
*/
|
|
if (warnIfAnalyticsMisconfigured() && isAnalyticsEnabled()) {
|
|
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN!, {
|
|
api_host: '/rl',
|
|
ui_host: 'https://eu.posthog.com',
|
|
defaults: '2026-05-30',
|
|
// Only create person profiles for users we actually identify: logged-out
|
|
// visitors stay anonymous and cheap.
|
|
person_profiles: 'identified_only',
|
|
persistence: 'memory',
|
|
capture_exceptions: true,
|
|
tracing_headers: tracingHosts(),
|
|
session_recording: {
|
|
maskAllInputs: true,
|
|
maskTextSelector: '*',
|
|
},
|
|
debug: process.env.NODE_ENV === 'development',
|
|
})
|
|
}
|