Skip to content

Commit a799743

Browse files
adeebshihadehclaude
andcommitted
add posthog for session analytics
Sentry for errors, PostHog for pass/fail analytics with device info. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 8836546 commit a799743

File tree

5 files changed

+152
-3
lines changed

5 files changed

+152
-3
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
VITE_SENTRY_DSN=https://[email protected]/4510604761825280
2+
VITE_PUBLIC_POSTHOG_KEY=phc_O4kXIsdyB2cm9Wne1pwJkj5jk9Ua51ABVPAhtSuYQ4V
3+
VITE_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com

package-lock.json

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@commaai/qdl": "git+https://github.com/commaai/qdl.js.git#21d7be79fa5178f253d32a0879bd8bdd4fa37e30",
1717
"@fontsource-variable/inter": "^5.2.5",
1818
"@fontsource-variable/jetbrains-mono": "^5.2.5",
19+
"posthog-js": "^1.310.1",
1920
"react": "^18.3.1",
2021
"react-dom": "^18.3.1",
2122
"xz-decompress": "^0.2.2"

src/app/Flash.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useRef, useState } from 'react'
2+
import posthog from 'posthog-js'
23
import { addBreadcrumb, setTags, setTag, setContext, captureSessionSummary } from '../utils/telemetry'
34

45
import { FlashManager, StepCode, ErrorCode, DeviceType } from '../utils/manager'
@@ -736,6 +737,20 @@ export default function Flash() {
736737
function sendSessionSummary(result) {
737738
if (reportSentRef.current) return
738739
reportSentRef.current = true
740+
741+
const errorName = Object.keys(ErrorCode).find(k => ErrorCode[k] === error) || 'NONE'
742+
const stepName = Object.keys(StepCode).find(k => StepCode[k] === step) || 'UNKNOWN'
743+
744+
// PostHog event
745+
posthog.capture('flash_session', {
746+
result,
747+
serial,
748+
device_type: selectedDevice,
749+
error_code: errorName,
750+
step: stepName,
751+
})
752+
753+
// Sentry (for errors with full context)
739754
const meta = {
740755
...buildEnvMeta(),
741756
selectedDevice,

src/main.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom/client'
3+
import posthog from 'posthog-js'
34
import { initSentry } from './utils/telemetry'
45

56
import '@fontsource-variable/inter'
@@ -8,6 +9,18 @@ import '@fontsource-variable/jetbrains-mono'
89
import './index.css'
910
import App from './app'
1011

12+
// Initialize PostHog
13+
if (import.meta.env.VITE_PUBLIC_POSTHOG_KEY) {
14+
posthog.init(import.meta.env.VITE_PUBLIC_POSTHOG_KEY, {
15+
api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST,
16+
capture_pageview: false, // We'll track manually
17+
persistence: 'memory', // Don't persist across sessions
18+
})
19+
}
20+
21+
// Initialize Sentry (no-op if DSN unset)
22+
initSentry()
23+
1124
// Explicitly load fonts before rendering to prevent FOUT
1225
async function loadFonts() {
1326
await Promise.all([
@@ -16,9 +29,6 @@ async function loadFonts() {
1629
])
1730
}
1831

19-
// Initialize telemetry (no-op if DSN unset)
20-
initSentry()
21-
2232
loadFonts().then(() => {
2333
ReactDOM.createRoot(document.getElementById('root')).render(
2434
<React.StrictMode>

0 commit comments

Comments
 (0)