66import { PostHog } from "posthog-node"
77import { app } from "electron"
88
9- // PostHog configuration from environment
10- const POSTHOG_DESKTOP_KEY = import . meta. env . MAIN_VITE_POSTHOG_KEY
9+ // PostHog configuration - hardcoded key for opensource users, env var override for internal builds
10+ // This enables analytics for all users including those building from source
11+ const POSTHOG_DESKTOP_KEY = import . meta. env . MAIN_VITE_POSTHOG_KEY || "phc_wM7gbrJhOLTvynyhnhPkrVGDc5mKRSXsLGQHqM3T3vq"
1112const POSTHOG_HOST = import . meta. env . MAIN_VITE_POSTHOG_HOST || "https://us.i.posthog.com"
1213
1314let posthog : PostHog | null = null
1415let currentUserId : string | null = null
1516let userOptedOut = false // Synced from renderer
1617
18+ // Cached user properties for analytics enrichment
19+ let cachedSubscriptionPlan : string | null = null
20+ let cachedConnectionMethod : string | null = null
21+
1722// Check if we're in development mode
1823// Set FORCE_ANALYTICS=true to test analytics in development
1924// Use a function to check lazily after app is ready
@@ -31,12 +36,15 @@ function isDev(): boolean {
3136 */
3237function getCommonProperties ( ) {
3338 return {
34- source : "desktop_main" ,
39+ source : "desktop" , // Unified source for desktop vs web analytics
3540 app_version : app . getVersion ( ) ,
3641 platform : process . platform ,
3742 arch : process . arch ,
3843 electron_version : process . versions . electron ,
3944 node_version : process . versions . node ,
45+ // Analytics enrichment properties
46+ subscription_plan : cachedSubscriptionPlan ,
47+ connection_method : cachedConnectionMethod ,
4048 }
4149}
4250
@@ -47,6 +55,21 @@ export function setOptOut(optedOut: boolean) {
4755 userOptedOut = optedOut
4856}
4957
58+ /**
59+ * Set subscription plan (called after fetching from API)
60+ */
61+ export function setSubscriptionPlan ( plan : string ) {
62+ cachedSubscriptionPlan = plan
63+ }
64+
65+ /**
66+ * Set connection method (called from renderer via IPC)
67+ * Values: "claude-subscription" | "api-key" | "custom-model"
68+ */
69+ export function setConnectionMethod ( method : string ) {
70+ cachedConnectionMethod = method
71+ }
72+
5073/**
5174 * Initialize PostHog for main process
5275 */
@@ -135,6 +158,9 @@ export function getCurrentUserId(): string | null {
135158 */
136159export function reset ( ) {
137160 currentUserId = null
161+ // Reset cached analytics properties
162+ cachedSubscriptionPlan = null
163+ cachedConnectionMethod = null
138164 // PostHog Node.js SDK doesn't have a reset method
139165 // Events will be sent as anonymous until next identify
140166}
@@ -192,11 +218,13 @@ export function trackWorkspaceCreated(workspace: {
192218 id : string
193219 projectId : string
194220 useWorktree : boolean
221+ repository ?: string
195222} ) {
196223 capture ( "workspace_created" , {
197224 workspace_id : workspace . id ,
198225 project_id : workspace . projectId ,
199226 use_worktree : workspace . useWorktree ,
227+ repository : workspace . repository ,
200228 } )
201229}
202230
@@ -223,12 +251,12 @@ export function trackWorkspaceDeleted(workspaceId: string) {
223251 */
224252export function trackMessageSent ( data : {
225253 workspaceId : string
226- messageLength : number
254+ subChatId ?: string
227255 mode : "plan" | "agent"
228256} ) {
229257 capture ( "message_sent" , {
230258 workspace_id : data . workspaceId ,
231- message_length : data . messageLength ,
259+ sub_chat_id : data . subChatId ,
232260 mode : data . mode ,
233261 } )
234262}
@@ -239,9 +267,41 @@ export function trackMessageSent(data: {
239267export function trackPRCreated ( data : {
240268 workspaceId : string
241269 prNumber : number
270+ repository ?: string
271+ mode ?: "worktree" | "local"
242272} ) {
243273 capture ( "pr_created" , {
244274 workspace_id : data . workspaceId ,
245275 pr_number : data . prNumber ,
276+ repository : data . repository ,
277+ mode : data . mode ,
278+ } )
279+ }
280+
281+ /**
282+ * Track commit created
283+ */
284+ export function trackCommitCreated ( data : {
285+ workspaceId : string
286+ filesChanged : number
287+ mode : "worktree" | "local"
288+ } ) {
289+ capture ( "commit_created" , {
290+ workspace_id : data . workspaceId ,
291+ files_changed : data . filesChanged ,
292+ mode : data . mode ,
293+ } )
294+ }
295+
296+ /**
297+ * Track sub-chat created
298+ */
299+ export function trackSubChatCreated ( data : {
300+ workspaceId : string
301+ subChatId : string
302+ } ) {
303+ capture ( "sub_chat_created" , {
304+ workspace_id : data . workspaceId ,
305+ sub_chat_id : data . subChatId ,
246306 } )
247307}
0 commit comments