-
Story
-
Resolution:
Unresolved
-
Undefined
-
None
-
None
-
Acceptance Criteria
- An initAnalytics() function is exported from src/analytics/index.ts.
- When ANALYTICS_ENABLED is true and a valid write key is present, it loads and initializes the Segment SDK with the configured write key and API URL.
- When disabled, initialization is skipped entirely — no SDK loaded, no network requests.
- The initialized Segment analytics instance is accessible to other modules that need it (e.g., via a getAnalytics() getter).
- Initialization is called in main.tsx alongside initMonitoring(), non-blocking, the app renders even if Segment fails.
- Errors during initialization are caught, logged, and reported to Sentry if available.
- Unit tests verify: successful init with valid config, SDK not loaded when disabled, graceful error handling on init failure.
Notes
- Use @segment/analytics-next (Analytics.js 2.0) as the SDK.
- Use dynamic import for code-splitting so the Segment SDK isn't in the main bundle when disabled.
- In main.tsx, run initMonitoring() and initAnalytics() in parallel with Promise.allSettled().
- getAnalytics() returns the instance or undefined — callers must handle the undefined case (analytics disabled or not yet initialized).