feat: implement analytics tracking for desktop app events

- Added event tracking for desktop app activation and quitting.
- Introduced analytics bridge in preload script to handle user identification and event capturing.
- Updated IPC channels to support analytics-related actions.
- Enhanced analytics functionality in the main process to track user interactions and application updates.
- Integrated analytics tracking for folder watching and deep link handling.
- Improved connector setup tracking in the web application.

This commit enhances the overall analytics capabilities of the application, ensuring better user behavior insights and event tracking across both desktop and web environments.
This commit is contained in:
DESKTOP-RTLN3BA\$punk 2026-04-18 14:35:14 -07:00
parent b38a297349
commit b440610e04
18 changed files with 673 additions and 80 deletions

View file

@ -3,14 +3,27 @@ import { machineIdSync } from 'node-machine-id';
import { app } from 'electron';
let client: PostHog | null = null;
let distinctId = '';
let machineId = '';
let currentDistinctId = '';
let identifiedUserId: string | null = null;
function baseProperties(): Record<string, unknown> {
return {
platform: 'desktop',
app_version: app.getVersion(),
os: process.platform,
arch: process.arch,
machine_id: machineId,
};
}
export function initAnalytics(): void {
const key = process.env.POSTHOG_KEY;
if (!key) return;
try {
distinctId = machineIdSync(true);
machineId = machineIdSync(true);
currentDistinctId = machineId;
} catch {
return;
}
@ -22,17 +35,92 @@ export function initAnalytics(): void {
});
}
export function trackEvent(event: string, properties?: Record<string, unknown>): void {
export function getMachineId(): string {
return machineId;
}
export function getDistinctId(): string {
return currentDistinctId;
}
/**
* Identify the current logged-in user in PostHog so main-process desktop
* events (and linked anonymous machine events) are attributed to that person.
*
* Idempotent: calling identify repeatedly with the same userId is a no-op.
*/
export function identifyUser(
userId: string,
properties?: Record<string, unknown>
): void {
if (!client || !userId) return;
if (identifiedUserId === userId) {
// Already identified — only refresh person properties
try {
client.identify({
distinctId: userId,
properties: {
...baseProperties(),
$set: {
...(properties || {}),
platform: 'desktop',
last_seen_at: new Date().toISOString(),
},
},
});
} catch {
// ignore
}
return;
}
try {
// Link the anonymous machine distinct ID to the authenticated user
client.identify({
distinctId: userId,
properties: {
...baseProperties(),
$anon_distinct_id: machineId,
$set: {
...(properties || {}),
platform: 'desktop',
last_seen_at: new Date().toISOString(),
},
$set_once: {
first_seen_platform: 'desktop',
},
},
});
identifiedUserId = userId;
currentDistinctId = userId;
} catch {
// Analytics must never break the app
}
}
/**
* Reset user identity on logout. Subsequent events are captured anonymously
* against the machine ID until the user logs in again.
*/
export function resetUser(): void {
if (!client) return;
identifiedUserId = null;
currentDistinctId = machineId;
}
export function trackEvent(
event: string,
properties?: Record<string, unknown>
): void {
if (!client) return;
try {
client.capture({
distinctId,
distinctId: currentDistinctId || machineId,
event,
properties: {
platform: 'desktop',
app_version: app.getVersion(),
os: process.platform,
...baseProperties(),
...properties,
},
});