Skip to content

Commit e1e259a

Browse files
committed
Initialize analytics within app
1 parent 338ee4f commit e1e259a

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

cli/src/index.tsx

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { handlePublish } from './commands/publish'
2323
import { runPlainLogin } from './login/plain-login'
2424
import { initializeApp } from './init/init-app'
2525
import { getProjectRoot, setProjectRoot } from './project-files'
26-
import { initAnalytics, trackEvent } from './utils/analytics'
26+
import { trackEvent } from './utils/analytics'
2727
import { getAuthToken, getAuthTokenDetails } from './utils/auth'
2828
import { resetCodebuffClient } from './utils/codebuff-client'
2929
import { setApiClientAuthToken } from './utils/codebuff-api'
@@ -66,7 +66,7 @@ function loadPackageVersion(): string {
6666
// Without this, refetchInterval won't work because TanStack Query thinks the app is "unfocused"
6767
focusManager.setEventListener(() => {
6868
// No-op: no event listeners in CLI environment (no window focus/visibility events)
69-
return () => {}
69+
return () => { }
7070
})
7171
focusManager.setFocused(true)
7272

@@ -222,26 +222,17 @@ async function main(): Promise<void> {
222222
const startCwd = process.cwd()
223223
const showProjectPicker = shouldShowProjectPicker(startCwd, homeDir)
224224

225-
// Initialize analytics early, before anything that might use the logger
226-
// (the logger calls trackEvent, which throws if analytics isn't initialized)
227-
try {
228-
initAnalytics()
229-
230-
// Track app launch event
231-
trackEvent(AnalyticsEvent.APP_LAUNCHED, {
232-
version: loadPackageVersion(),
233-
platform: process.platform,
234-
arch: process.arch,
235-
hasInitialPrompt: Boolean(initialPrompt),
236-
hasAgentOverride: hasAgentOverride,
237-
continueChat,
238-
initialMode: initialMode ?? 'DEFAULT',
239-
isFreeBuff: IS_FREEBUFF,
240-
})
241-
} catch (error) {
242-
// Analytics initialization is optional - don't fail the app if it errors
243-
logger.debug(error, 'Failed to initialize analytics')
244-
}
225+
// Requires analytics to be initialized, which is done in initializeApp
226+
trackEvent(AnalyticsEvent.APP_LAUNCHED, {
227+
version: loadPackageVersion(),
228+
platform: process.platform,
229+
arch: process.arch,
230+
hasInitialPrompt: Boolean(initialPrompt),
231+
hasAgentOverride: hasAgentOverride,
232+
continueChat,
233+
initialMode: initialMode ?? 'DEFAULT',
234+
isFreeBuff: IS_FREEBUFF,
235+
})
245236

246237
// Initialize agent registry (loads user agents via SDK).
247238
// When --agent is provided, skip local .agents to avoid overrides.

cli/src/init/init-app.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { initializeThemeStore } from '../hooks/use-theme'
1212
import { setProjectRoot } from '../project-files'
1313
import { initTimestampFormatter } from '../utils/helpers'
1414
import { enableManualThemeRefresh } from '../utils/theme-system'
15+
import { initAnalytics } from '../utils/analytics'
1516
import { initializeDirenv } from './init-direnv'
1617

1718
export async function initializeApp(params: { cwd?: string }): Promise<void> {
@@ -21,6 +22,14 @@ export async function initializeApp(params: { cwd?: string }): Promise<void> {
2122
const baseCwd = process.cwd()
2223
setProjectRoot(baseCwd)
2324

25+
// Initialize analytics before direnv, because direnv uses the logger
26+
// which calls trackEvent — analytics must be ready first.
27+
try {
28+
initAnalytics()
29+
} catch (error) {
30+
console.debug('Failed to initialize analytics:', error)
31+
}
32+
2433
// Initialize direnv environment before anything else
2534
initializeDirenv()
2635

0 commit comments

Comments
 (0)