mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-06 06:12:40 +02:00
Add PostHog analytics to desktop main process
This commit is contained in:
parent
e85c355592
commit
8566b03c91
7 changed files with 75 additions and 1 deletions
46
surfsense_desktop/src/modules/analytics.ts
Normal file
46
surfsense_desktop/src/modules/analytics.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { PostHog } from 'posthog-node';
|
||||
import { machineIdSync } from 'node-machine-id';
|
||||
import { app } from 'electron';
|
||||
|
||||
let client: PostHog | null = null;
|
||||
let distinctId = '';
|
||||
|
||||
export function initAnalytics(): void {
|
||||
const key = process.env.POSTHOG_KEY;
|
||||
if (!key) return;
|
||||
|
||||
try {
|
||||
distinctId = machineIdSync(true);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
client = new PostHog(key, {
|
||||
host: process.env.POSTHOG_HOST || 'https://us.i.posthog.com',
|
||||
flushAt: 20,
|
||||
flushInterval: 10000,
|
||||
});
|
||||
}
|
||||
|
||||
export function trackEvent(event: string, properties?: Record<string, unknown>): void {
|
||||
if (!client) return;
|
||||
|
||||
client.capture({
|
||||
distinctId,
|
||||
event,
|
||||
properties: {
|
||||
platform: 'desktop',
|
||||
app_version: app.getVersion(),
|
||||
os: process.platform,
|
||||
...properties,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function shutdownAnalytics(): Promise<void> {
|
||||
if (!client) return;
|
||||
|
||||
const timeout = new Promise<void>((resolve) => setTimeout(resolve, 3000));
|
||||
await Promise.race([client.shutdown(), timeout]);
|
||||
client = null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue