feat: add PostHog telemetry and privacy toggle in settings (#409)

This commit is contained in:
paisley
2026-03-11 15:22:02 +08:00
committed by GitHub
Unverified
parent d11e266cbb
commit da8ed3bb32
9 changed files with 182 additions and 121 deletions

View File

@@ -25,7 +25,10 @@ export interface AppSettings {
language: string;
startMinimized: boolean;
launchAtStartup: boolean;
telemetryEnabled: boolean;
machineId: string;
hasReportedInstall: boolean;
// Gateway
gatewayAutoStart: boolean;
gatewayPort: number;
@@ -36,17 +39,17 @@ export interface AppSettings {
proxyHttpsServer: string;
proxyAllServer: string;
proxyBypassRules: string;
// Update
updateChannel: 'stable' | 'beta' | 'dev';
autoCheckUpdate: boolean;
autoDownloadUpdate: boolean;
skippedVersions: string[];
// UI State
sidebarCollapsed: boolean;
devModeUnlocked: boolean;
// Presets
selectedBundles: string[];
enabledSkills: string[];
@@ -62,7 +65,10 @@ const defaults: AppSettings = {
language: 'en',
startMinimized: false,
launchAtStartup: false,
telemetryEnabled: true,
machineId: '',
hasReportedInstall: false,
// Gateway
gatewayAutoStart: true,
gatewayPort: 18789,
@@ -73,17 +79,17 @@ const defaults: AppSettings = {
proxyHttpsServer: '',
proxyAllServer: '',
proxyBypassRules: '<local>;localhost;127.0.0.1;::1',
// Update
updateChannel: 'stable',
autoCheckUpdate: true,
autoDownloadUpdate: false,
skippedVersions: [],
// UI State
sidebarCollapsed: false,
devModeUnlocked: false,
// Presets
selectedBundles: ['productivity', 'developer'],
enabledSkills: [],

View File

@@ -0,0 +1,79 @@
import { PostHog } from 'posthog-node';
import { machineIdSync } from 'node-machine-id';
import { app } from 'electron';
import { getSetting, setSetting } from './store';
import { logger } from './logger';
const POSTHOG_API_KEY = 'phc_aGNegeJQP5FzNiF2rEoKqQbkuCpiiETMttplibXpB0n';
const POSTHOG_HOST = 'https://us.i.posthog.com';
let posthogClient: PostHog | null = null;
let distinctId: string = '';
/**
* Initialize PostHog telemetry
*/
export async function initTelemetry(): Promise<void> {
try {
const telemetryEnabled = await getSetting('telemetryEnabled');
if (!telemetryEnabled) {
logger.info('Telemetry is disabled in settings');
return;
}
// Initialize PostHog client
posthogClient = new PostHog(POSTHOG_API_KEY, { host: POSTHOG_HOST });
// Get or generate machine ID
distinctId = await getSetting('machineId');
if (!distinctId) {
distinctId = machineIdSync();
await setSetting('machineId', distinctId);
logger.debug(`Generated new machine ID for telemetry: ${distinctId}`);
}
// Common properties for all events
const properties = {
$app_version: app.getVersion(),
$os: process.platform,
arch: process.arch,
};
// Check if this is a new installation
const hasReportedInstall = await getSetting('hasReportedInstall');
if (!hasReportedInstall) {
posthogClient.capture({
distinctId,
event: 'app_installed',
properties,
});
await setSetting('hasReportedInstall', true);
logger.info('Reported app_installed event');
}
// Always report app opened
posthogClient.capture({
distinctId,
event: 'app_opened',
properties,
});
logger.debug('Reported app_opened event');
} catch (error) {
logger.error('Failed to initialize telemetry:', error);
}
}
/**
* Ensure PostHog flushes all pending events before shutting down
*/
export async function shutdownTelemetry(): Promise<void> {
if (posthogClient) {
try {
await posthogClient.shutdown();
logger.debug('Flushed telemetry events on shutdown');
} catch (error) {
logger.error('Error shutting down telemetry:', error);
}
}
}