Commit 3800fcd
committed
fix(analytics): guard PostHog session-recording APIs to prevent crash
Some users saw a crash: "window.posthog.stopSessionRecording is not a function".
Different PostHog bundles/plugins expose session recording controls differently:
- Some expose start/stop on the root (posthog.startSessionRecording / stopSessionRecording)
- Others expose them under posthog.sessionRecording.startRecording / stopRecording
- In all versions, recording can be toggled via set_config({ disable_session_recording })
Changes
- Hardened enable/disable logic in [src/components/PostHogProvider/index.tsx](src/components/PostHogProvider/index.tsx):
- Flexible interface: [PostHogInstance](src/components/PostHogProvider/index.tsx:9) with optional methods for multiple SDK shapes
- Consent-driven control via [PostHogProvider()](src/components/PostHogProvider/index.tsx:31)
- [enablePostHog()](src/components/PostHogProvider/index.tsx:54):
- Best-effort ph.opt_in_capturing() (try/catch)
- Use ph.set_config?.({ disable_session_recording: false })
- Attempt ph.startSessionRecording?.() and ph.sessionRecording?.startRecording?.()
- [disablePostHog()](src/components/PostHogProvider/index.tsx:77):
- Use ph.set_config?.({ disable_session_recording: true })
- Attempt ph.stopSessionRecording?.() and ph.sessionRecording?.stopRecording?.()
- Best-effort ph.opt_out_capturing() (try/catch)
- All calls guarded against SSR (typeof window) and undefined window.posthog
Why
- Avoids invoking undefined helpers in environments where the adapter/plugin does not expose them
- set_config({ disable_session_recording }) is widely supported and reliably toggles recording
- Preserves existing consent flow using [hasConsent()](src/lib/analytics/consent-manager.ts:38) and [onConsentChange()](src/lib/analytics/consent-manager.ts:48)
User impact
- Eliminates the crash while maintaining the intended consent behavior
Files touched
- [src/components/PostHogProvider/index.tsx](src/components/PostHogProvider/index.tsx)
Notes
- Editor TS errors for '@roo-code/types', 'react-cookie-consent', and 'tldts' are due to missing local node_modules; they are declared in package.json and resolve in CI/local installs.
Test plan
- Load with consent denied: set_config disables recording; no start/stop calls; no crash
- Accept consent via banner: set_config enables recording; start methods called if present; no crash
- Decline after accepting: set_config disables recording; stop methods called if present; no crash
- Navigate/refresh to ensure stability across routes and SDK variants1 parent 05feff6 commit 3800fcd
1 file changed
+54
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
5 | 9 | | |
6 | 10 | | |
7 | 11 | | |
8 | | - | |
9 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
10 | 23 | | |
11 | 24 | | |
12 | 25 | | |
| |||
39 | 52 | | |
40 | 53 | | |
41 | 54 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
46 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
47 | 75 | | |
48 | 76 | | |
49 | 77 | | |
50 | | - | |
51 | | - | |
52 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
53 | 97 | | |
54 | 98 | | |
55 | 99 | | |
| |||
0 commit comments