Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix ph in sdk

Louis Beaumont4f2e305d14f2
diff --git a/screenpipe-js/browser-sdk/src/index.ts b/screenpipe-js/browser-sdk/src/index.ts
index f1dcb39a5..faaef7127 100644
--- a/screenpipe-js/browser-sdk/src/index.ts
+++ b/screenpipe-js/browser-sdk/src/index.ts
@@ -10,11 +10,7 @@ import type {
VisionStreamResponse,
} from "../../common/types";
import { toSnakeCase, convertToCamelCase } from "../../common/utils";
-import {
- captureEvent,
- captureMainFeatureEvent,
- identifyUser,
-} from "../../common/analytics";
+import { captureEvent, captureMainFeatureEvent } from "../../common/analytics";
async function sendInputControl(action: InputAction): Promise<boolean> {
const apiUrl = "http://localhost:3030";
@@ -65,11 +61,10 @@ export interface BrowserPipe {
}
class BrowserPipeImpl implements BrowserPipe {
- private userProperties?: Record<string, any>;
-
private async initAnalyticsIfNeeded(): Promise<{
analyticsEnabled: boolean;
userId?: string;
+ email?: string;
}> {
try {
// Connect to settings SSE stream
@@ -81,6 +76,7 @@ class BrowserPipeImpl implements BrowserPipe {
const settings = await new Promise<{
analyticsEnabled: boolean;
userId?: string;
+ email?: string;
}>((resolve, reject) => {
const timeout = setTimeout(() => {
settingsStream.close();
@@ -98,7 +94,10 @@ class BrowserPipeImpl implements BrowserPipe {
const userId =
settingsArray.find(([key]) => key === "user.clerk_id")?.[1] ??
undefined;
- resolve({ analyticsEnabled, userId });
+ const userEmail =
+ settingsArray.find(([key]) => key === "user.email")?.[1] ??
+ undefined;
+ resolve({ analyticsEnabled, userId, email: userEmail });
};
settingsStream.onerror = (error) => {
@@ -108,12 +107,10 @@ class BrowserPipeImpl implements BrowserPipe {
};
});
- if (settings.analyticsEnabled && settings.userId) {
- await identifyUser(settings.userId, this.userProperties);
- }
return {
analyticsEnabled: settings.analyticsEnabled,
userId: settings.userId,
+ email: settings.email,
};
} catch (error) {
console.error(
@@ -130,7 +127,7 @@ class BrowserPipeImpl implements BrowserPipe {
async sendDesktopNotification(
options: NotificationOptions
): Promise<boolean> {
- await this.initAnalyticsIfNeeded();
+ const { userId, email } = await this.initAnalyticsIfNeeded();
const notificationApiUrl = "http://localhost:11435";
try {
await fetch(`${notificationApiUrl}/notify`, {
@@ -138,12 +135,18 @@ class BrowserPipeImpl implements BrowserPipe {
headers: { "Content-Type": "application/json" },
body: JSON.stringify(options),
});
- await this.captureEvent("notification_sent", { success: true });
+ await this.captureEvent("notification_sent", {
+ distinct_id: userId,
+ email: email,
+ success: true,
+ });
return true;
} catch (error) {
await this.captureEvent("error_occurred", {
feature: "notification",
error: "send_failed",
+ distinct_id: userId,
+ email: email,
});
return false;
}
@@ -153,7 +156,7 @@ class BrowserPipeImpl implements BrowserPipe {
params: ScreenpipeQueryParams
): Promise<ScreenpipeResponse | null> {
console.log("queryScreenpipe:", params);
- await this.initAnalyticsIfNeeded();
+ const { userId, email } = await this.initAnalyticsIfNeeded();
const queryParams = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined && value !== "") {
@@ -190,14 +193,18 @@ class BrowserPipeImpl implements BrowserPipe {
}
const data = await response.json();
await captureEvent("search_performed", {
+ distinct_id: userId,
content_type: params.contentType,
result_count: data.pagination.total,
+ email: email,
});
return convertToCamelCase(data) as ScreenpipeResponse;
} catch (error) {
await captureEvent("error_occurred", {
feature: "search",
error: "query_failed",
+ distinct_id: userId,
+ email: email,
});
console.error("error querying screenpipe:", error);
return null;
@@ -226,10 +233,13 @@ class BrowserPipeImpl implements BrowserPipe {
const eventSource = new EventSource(
"http://localhost:3030/sse/transcriptions"
);
+ const { userId, email } = await this.initAnalyticsIfNeeded();
try {
await this.captureEvent("stream_started", {
feature: "transcription",
+ distinct_id: userId,
+ email: email,
});
while (true) {
@@ -269,6 +279,8 @@ class BrowserPipeImpl implements BrowserPipe {
} finally {
await this.captureEvent("stream_ended", {
feature: "transcription",
+ distinct_id: userId,
+ email: email,
});
eventSource.close();
}
@@ -280,9 +292,12 @@ class BrowserPipeImpl implements BrowserPipe {
const eventSource = new EventSource(
`http://localhost:3030/sse/vision?images=${includeImages}`
);
+ const { userId, email } = await this.initAnalyticsIfNeeded();
try {
await this.captureEvent("stream_started", {
feature: "vision",
+ distinct_id: userId,
+ email: email,
});
while (true) {
@@ -303,6 +318,8 @@ class BrowserPipeImpl implements BrowserPipe {
} finally {
await this.captureEvent("stream_ended", {
feature: "vision",
+ distinct_id: userId,
+ email: email,
});
eventSource.close();
}
diff --git a/screenpipe-js/common/analytics.ts b/screenpipe-js/common/analytics.ts
index bd99165dd..f31fe17d6 100644
--- a/screenpipe-js/common/analytics.ts
+++ b/screenpipe-js/common/analytics.ts
@@ -5,36 +5,35 @@ const POSTHOG_HOST = "https://eu.i.posthog.com";
let initialized = false;
-function initPosthog() {
+function initPosthog(userId?: string, email?: string) {
if (!initialized) {
- posthog.init(POSTHOG_KEY, { api_host: POSTHOG_HOST });
+ posthog.init(POSTHOG_KEY, {
+ api_host: POSTHOG_HOST,
+ distinct_id: userId,
+ email: email,
+ });
+ posthog.identify(userId, { email: email });
initialized = true;
}
}
-export async function identifyUser(
- userId: string,
- properties?: Record<string, any>
-): Promise<void> {
- initPosthog();
- posthog.identify(userId, properties);
-}
-
export async function captureEvent(
name: string,
properties?: Record<string, any>
): Promise<void> {
- initPosthog();
- posthog.capture(name, properties);
+ initPosthog(properties?.distinct_id, properties?.email);
+ const { distinct_id, ...restProperties } = properties || {};
+ posthog.capture(name, restProperties);
}
export async function captureMainFeatureEvent(
name: string,
properties?: Record<string, any>
): Promise<void> {
- initPosthog();
+ initPosthog(properties?.distinct_id, properties?.email);
+ const { distinct_id, ...restProperties } = properties || {};
posthog.capture(name, {
feature: "main",
- ...properties,
+ ...restProperties,
});
}
diff --git a/screenpipe-js/node-sdk/src/index.ts b/screenpipe-js/node-sdk/src/index.ts
index 3b2c7af01..2516b86f2 100644
--- a/screenpipe-js/node-sdk/src/index.ts
+++ b/screenpipe-js/node-sdk/src/index.ts
@@ -13,17 +13,11 @@ import { toSnakeCase, convertToCamelCase } from "../../common/utils";
import { SettingsManager } from "./SettingsManager";
import { InboxManager } from "./InboxManager";
import { EventSource } from "eventsource";
-import {
- captureEvent,
- captureMainFeatureEvent,
- identifyUser,
-} from "../../common/analytics";
+import { captureEvent, captureMainFeatureEvent } from "../../common/analytics";
class NodePipe {
private analyticsInitialized = false;
private analyticsEnabled = true;
- private userId?: string;
- private userProperties?: Record<string, any>;
public input = {
type: (text: string) =>
@@ -223,12 +217,11 @@ class NodePipe {
}
private async initAnalyticsIfNeeded() {
- if (this.analyticsInitialized || !this.userId) return;
+ if (this.analyticsInitialized) return;
const settings = await this.settings.getAll();
this.analyticsEnabled = settings.analyticsEnabled;
if (settings.analyticsEnabled) {
- await identifyUser(this.userId, this.userProperties);
this.analyticsInitialized = true;
}
}
@@ -239,7 +232,12 @@ class NodePipe {
) {
if (!this.analyticsEnabled) return;
await this.initAnalyticsIfNeeded();
- return captureEvent(eventName, properties);
+ const settings = await this.settings.getAll();
+ return captureEvent(eventName, {
+ distinct_id: settings.user.id,
+ email: settings.user.email,
+ ...properties,
+ });
}
public async captureMainFeatureEvent(