()
| 45 | * Initialize PostHog telemetry |
| 46 | */ |
| 47 | export async function initTelemetry(): Promise<void> { |
| 48 | try { |
| 49 | const telemetryEnabled = await getSetting('telemetryEnabled'); |
| 50 | if (!telemetryEnabled) { |
| 51 | logger.info('Telemetry is disabled in settings'); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Initialize PostHog client |
| 56 | posthogClient = new PostHog(POSTHOG_API_KEY, { host: POSTHOG_HOST }); |
| 57 | |
| 58 | // Get or generate machine ID |
| 59 | distinctId = await getSetting('machineId'); |
| 60 | if (!distinctId) { |
| 61 | distinctId = machineIdSync(); |
| 62 | await setSetting('machineId', distinctId); |
| 63 | logger.debug(`Generated new machine ID for telemetry: ${distinctId}`); |
| 64 | } |
| 65 | |
| 66 | const properties = getCommonProperties(); |
| 67 | |
| 68 | // Check if this is a new installation |
| 69 | const hasReportedInstall = await getSetting('hasReportedInstall'); |
| 70 | if (!hasReportedInstall) { |
| 71 | posthogClient.capture({ |
| 72 | distinctId, |
| 73 | event: 'app_installed', |
| 74 | properties, |
| 75 | }); |
| 76 | await setSetting('hasReportedInstall', true); |
| 77 | logger.info('Reported app_installed event'); |
| 78 | } |
| 79 | |
| 80 | // Always report app opened |
| 81 | posthogClient.capture({ |
| 82 | distinctId, |
| 83 | event: 'app_opened', |
| 84 | properties, |
| 85 | }); |
| 86 | logger.debug('Reported app_opened event'); |
| 87 | |
| 88 | } catch (error) { |
| 89 | logger.error('Failed to initialize telemetry:', error); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | export function trackMetric(event: string, properties: Record<string, unknown> = {}): void { |
| 94 | logger.info(`[metric] ${event}`, properties); |
no test coverage detected