Skip to content

Commit 5d2d68e

Browse files
committed
fix (analytics): made posthog optional
1 parent 12f8cd6 commit 5d2d68e

14 files changed

Lines changed: 91 additions & 47 deletions

File tree

‎src/client/.env.selfhost.template‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ AUTH0_ISSUER_BASE_URL=""
2929
AUTH0_CLIENT_ID=""
3030
AUTH0_CLIENT_SECRET=""
3131
AUTH0_AUDIENCE=""
32-
AUTH0_SCOPE=""
32+
AUTH0_SCOPE=""
33+
34+
# --- Analytics (Optional) ---
35+
# If you want to enable product analytics, provide your PostHog project key.
36+
NEXT_PUBLIC_POSTHOG_KEY=
37+
# If using a self-hosted PostHog instance, provide the host URL. Otherwise, leave it empty to default to PostHog's US cloud.
38+
NEXT_PUBLIC_POSTHOG_HOST=
39+
# --- Analytics (Optional) ---
40+
# If you want to enable product analytics, provide your PostHog project key.
41+
NEXT_PUBLIC_POSTHOG_KEY=
42+

‎src/client/.env.template‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ AUTH0_ISSUER_BASE_URL="" # Your Auth0 domain with protocol, e.g. "https://YOUR_T
1111
AUTH0_CLIENT_ID=
1212
AUTH0_CLIENT_SECRET=
1313
AUTH0_AUDIENCE=
14-
AUTH0_SCOPE='openid profile email offline_access read:chat write:chat read:profile write:profile manage:google_auth read:tasks write:tasks read:notifications read:config write:config admin:user_metadata read:contacts write:contacts'
14+
AUTH0_SCOPE='openid profile email offline_access read:chat write:chat read:profile write:profile manage:google_auth read:tasks write:tasks read:notifications read:config write:config admin:user_metadata read:contacts write:contacts'
15+
16+
# --- Analytics (Optional) ---
17+
# If you want to enable product analytics, provide your PostHog project key.
18+
NEXT_PUBLIC_POSTHOG_KEY=
19+
# If using a self-hosted PostHog instance, provide the host URL. Otherwise, leave it empty to default to PostHog's US cloud.
20+
NEXT_PUBLIC_POSTHOG_HOST=
21+
# --- Analytics (Optional) ---
22+
# If you want to enable product analytics, provide your PostHog project key.
23+
NEXT_PUBLIC_POSTHOG_KEY=
24+

‎src/client/Dockerfile‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ ARG AUTH0_CLIENT_ID
2020
ARG AUTH0_CLIENT_SECRET
2121
ARG AUTH0_AUDIENCE
2222
ARG AUTH0_SCOPE
23+
ARG NEXT_PUBLIC_POSTHOG_KEY
24+
ARG NEXT_PUBLIC_POSTHOG_HOST
25+
ARG NEXT_PUBLIC_POSTHOG_KEY
2326

2427
# Set them as environment variables for the build process
2528
ENV NEXT_PUBLIC_APP_SERVER_URL=$NEXT_PUBLIC_APP_SERVER_URL
@@ -34,6 +37,9 @@ ENV AUTH0_CLIENT_ID=$AUTH0_CLIENT_ID
3437
ENV AUTH0_CLIENT_SECRET=$AUTH0_CLIENT_SECRET
3538
ENV AUTH0_AUDIENCE=$AUTH0_AUDIENCE
3639
ENV AUTH0_SCOPE=$AUTH0_SCOPE
40+
ENV NEXT_PUBLIC_POSTHOG_KEY=$NEXT_PUBLIC_POSTHOG_KEY
41+
ENV NEXT_PUBLIC_POSTHOG_HOST=$NEXT_PUBLIC_POSTHOG_HOST
42+
ENV NEXT_PUBLIC_POSTHOG_KEY=$NEXT_PUBLIC_POSTHOG_KEY
3743
# --------------------------
3844

3945
# Copy package.json and lock file to leverage Docker cache

‎src/client/app/home/page.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ const HomePage = () => {
411411
const errorData = await response.json()
412412
throw new Error(errorData.error || "Approval failed")
413413
}
414-
posthog.capture("task_approved", { task_id: taskId })
414+
posthog?.capture("task_approved", { task_id: taskId })
415415
toast.success("Plan approved! Task has been queued for execution.")
416416
fetchData() // Refresh data
417417
} catch (error) {
@@ -433,7 +433,7 @@ const HomePage = () => {
433433
})
434434
if (!response.ok) throw new Error("Failed to delete task")
435435
if (taskToDelete?.status === "approval_pending") {
436-
posthog.capture("task_disapproved", { task_id: taskId })
436+
posthog?.capture("task_disapproved", { task_id: taskId })
437437
}
438438
toast.success("Task deleted successfully!")
439439
fetchData()

‎src/client/app/integrations/page.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const ManualTokenEntryModal = ({ integration, onClose, onSuccess }) => {
121121
)
122122
}
123123

124-
posthog.capture("integration_connected", {
124+
posthog?.capture("integration_connected", {
125125
integration_name: integration.name,
126126
auth_type: "manual"
127127
})
@@ -667,7 +667,7 @@ const IntegrationsPage = () => {
667667
)
668668
if (!response.ok)
669669
throw new Error(`Failed to disconnect ${displayName}`)
670-
posthog.capture("integration_disconnected", {
670+
posthog?.capture("integration_disconnected", {
671671
integration_name: integrationName
672672
})
673673
toast.success(`${displayName} disconnected.`)
@@ -689,7 +689,7 @@ const IntegrationsPage = () => {
689689
if (success) {
690690
const capitalized =
691691
success.charAt(0).toUpperCase() + success.slice(1)
692-
posthog.capture("integration_connected", {
692+
posthog?.capture("integration_connected", {
693693
integration_name: success,
694694
auth_type: "oauth"
695695
})
@@ -808,4 +808,4 @@ const IntegrationsPage = () => {
808808
)
809809
}
810810

811-
export default IntegrationsPage
811+
export default IntegrationsPage

‎src/client/app/journal/page.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ const OrganizerPage = () => {
246246
const errorData = await response.json()
247247
throw new Error(errorData.error || "Approval failed")
248248
}
249-
posthog.capture("task_approved", { task_id: taskId })
249+
posthog?.capture("task_approved", { task_id: taskId })
250250
toast.success("Plan approved! Task has been queued for execution.")
251251
refreshData()
252252
} catch (error) {
@@ -309,7 +309,7 @@ const OrganizerPage = () => {
309309
})
310310
if (!response.ok) throw new Error((await response.json()).error)
311311
if (taskToDelete?.status === "approval_pending") {
312-
posthog.capture("task_disapproved", { task_id: taskId })
312+
posthog?.capture("task_disapproved", { task_id: taskId })
313313
}
314314
toast.success("Task deleted successfully!")
315315
refreshData()
@@ -332,7 +332,7 @@ const OrganizerPage = () => {
332332
})
333333
})
334334
if (!response.ok) throw new Error((await response.json()).error)
335-
posthog.capture("task_edited", {
335+
posthog?.capture("task_edited", {
336336
task_id: editingTask.task_id,
337337
is_recurring: editingTask.schedule?.type === "recurring"
338338
})

‎src/client/app/onboarding/page.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ const OnboardingPage = () => {
312312
body: JSON.stringify({ whatsapp_number: whatsappNumber })
313313
})
314314
}
315-
posthog.capture("onboarding_completed")
315+
posthog?.capture("onboarding_completed")
316316
setStage("whatsNext") // Go to the new "What's Next" screen
317317
// setTimeout(() => router.push("/home"), 2500) // Removed auto-redirect
318318
} catch (error) {

‎src/client/app/tasks/page.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ const Tasks = () => {
312312
throw new Error(data.error || "Failed to add task")
313313
}
314314
toast.success("New plan created successfully!")
315-
posthog.capture("workflow_created", {
315+
posthog?.capture("workflow_created", {
316316
plan_steps: taskData.plan.length,
317317
is_recurring: taskData.schedule.type === "recurring",
318318
priority: taskData.priority
@@ -370,7 +370,7 @@ const Tasks = () => {
370370
})
371371
})
372372
if (!response.ok) throw new Error((await response.json()).error)
373-
posthog.capture("task_edited", {
373+
posthog?.capture("task_edited", {
374374
task_id: editingTask.task_id,
375375
is_recurring: editingTask.schedule?.type === "recurring"
376376
})
@@ -395,7 +395,7 @@ const Tasks = () => {
395395
toast.success("Task deleted!")
396396
setViewingTask(null)
397397
if (taskToDelete?.status === "approval_pending") {
398-
posthog.capture("task_disapproved", { task_id: taskId })
398+
posthog?.capture("task_disapproved", { task_id: taskId })
399399
}
400400
await fetchTasksData()
401401
} catch (error) {
@@ -416,7 +416,7 @@ const Tasks = () => {
416416
toast.error(errorData.detail, { duration: 6000 })
417417
else throw new Error(errorData.error || "Approval failed")
418418
} else {
419-
posthog.capture("task_approved", { task_id: taskId })
419+
posthog?.capture("task_approved", { task_id: taskId })
420420
toast.success("Plan approved and queued for execution.")
421421
}
422422
setViewingTask(null)
@@ -438,7 +438,7 @@ const Tasks = () => {
438438
body: JSON.stringify({ taskId })
439439
})
440440
if (!response.ok) throw new Error((await response.json()).error)
441-
posthog.capture("task_rerun", { original_task_id: taskId })
441+
posthog?.capture("task_rerun", { original_task_id: taskId })
442442
toast.success("Task duplicated for approval.")
443443
await fetchTasksData()
444444
} catch (error) {

‎src/client/components/ChatOverlay.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const ChatOverlay = ({ onClose }) => {
7474

7575
const sendMessage = async () => {
7676
if (input.trim() === "") return
77-
posthog.capture("chat_message_sent", {
77+
posthog?.capture("chat_message_sent", {
7878
message_length: input.length
7979
})
8080
const newUserMessage = {

‎src/client/components/PostHogProvider.jsx‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import { PostHogProvider as PHProvider } from "posthog-js/react"
55
import { useEffect } from "react"
66

77
export function PostHogProvider({ children }) {
8-
useEffect(() => {
9-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
10-
api_host: "/ingest",
11-
ui_host: "https://us.posthog.com",
12-
defaults: '2025-05-24',
13-
capture_exceptions: true,
14-
debug: process.env.NODE_ENV === "development",
15-
})
16-
}, [])
8+
useEffect(() => {
9+
if (process.env.NEXT_PUBLIC_POSTHOG_KEY) {
10+
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
11+
api_host: "/ingest",
12+
ui_host:
13+
process.env.NEXT_PUBLIC_POSTHOG_HOST ||
14+
"https://us.posthog.com",
15+
defaults: "2025-05-24",
16+
capture_exceptions: true,
17+
debug: process.env.NODE_ENV === "development"
18+
})
19+
}
20+
}, [])
1721

18-
return <PHProvider client={posthog}>{children}</PHProvider>
22+
if (!process.env.NEXT_PUBLIC_POSTHOG_KEY) return <>{children}</>
23+
return <PHProvider client={posthog}>{children}</PHProvider>
1924
}

0 commit comments

Comments
 (0)