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(app): gate posthog, sentry on analytics preference (#5214)

Harsh Kumar16bb5f537ebf
diff --git a/apps/screenpipe-app-tauri/app/providers.tsx b/apps/screenpipe-app-tauri/app/providers.tsx
index 8b32833eb..ef199dcb6 100644
--- a/apps/screenpipe-app-tauri/app/providers.tsx
+++ b/apps/screenpipe-app-tauri/app/providers.tsx
@@ -17,7 +17,7 @@ import { useUpdateListener } from "@/components/update-banner";
import { AppEntitlementGate } from "@/components/app-entitlement-gate";
import { DeeplinkHandler } from "@/components/deeplink-handler";
import { usePathname } from "next/navigation";
-import { readCachedAnalyticsId } from "@/lib/analytics-id";
+import { readCachedAnalyticsId, readCachedAnalyticsEnabled } from "@/lib/analytics-id";
import { QueryClientProvider } from "@tanstack/react-query";
import { queryClient } from "@/lib/query-client";
@@ -69,6 +69,9 @@ export const Providers = forwardRef<
// plus pollute prod analytics with test traffic.
const isE2E = process.env.NEXT_PUBLIC_SCREENPIPE_E2E === "true";
if (isDebug || isE2E) return;
+ // Read the cached analytics preference to sync PostHog opt-in/out
+ // after init. undefined = first boot → allow capturing (default true).
+ const cachedEnabled = readCachedAnalyticsEnabled();
// Bootstrap with the stable per-install id (mirrors settings.analyticsId,
// cached by the identify() effect in use-settings) so EVERY event — incl.
// ones fired by overlay windows like the floating search bar before the
@@ -86,6 +89,12 @@ export const Providers = forwardRef<
? { bootstrap: { distinctID: cachedAnalyticsId, isIdentifiedID: true } }
: {}),
});
+ // sync opt-in/out with cached preference on every boot
+ if (cachedEnabled === false) {
+ posthog.opt_out_capturing();
+ } else {
+ posthog.opt_in_capturing();
+ }
}
}, []);
diff --git a/apps/screenpipe-app-tauri/components/settings/privacy-section.tsx b/apps/screenpipe-app-tauri/components/settings/privacy-section.tsx
index 34446ed05..a962e73a9 100644
--- a/apps/screenpipe-app-tauri/components/settings/privacy-section.tsx
+++ b/apps/screenpipe-app-tauri/components/settings/privacy-section.tsx
@@ -60,6 +60,7 @@ import { commands } from "@/lib/utils/tauri";
import posthog from "posthog-js";
import * as Sentry from "@sentry/react";
import { defaultOptions } from "tauri-plugin-sentry-api";
+import { cacheAnalyticsEnabled } from "@/lib/analytics-id";
import {
validateField,
sanitizeValue,
@@ -590,6 +591,10 @@ export function PrivacySection() {
const analyticsEnabled =
pendingSettings.analyticsEnabled ?? settings.analyticsEnabled;
+ // Cache immediately so the next boot picks up the change before
+ // settings IPC resolves (see readCachedAnalyticsEnabled in providers.tsx).
+ cacheAnalyticsEnabled(analyticsEnabled);
+
if (!analyticsEnabled) {
posthog.capture("telemetry", { enabled: false });
posthog.opt_out_capturing();
@@ -885,7 +890,17 @@ export function PrivacySection() {
};
const handleAnalyticsToggle = (checked: boolean) => {
- handleSettingsChange({ analyticsEnabled: checked }, true);
+ // no restart needed — analytics is purely frontend
+ handleSettingsChange({ analyticsEnabled: checked }, false);
+ cacheAnalyticsEnabled(checked);
+ const isDebug = process.env.TAURI_ENV_DEBUG === "true";
+ if (!isDebug) {
+ if (checked) {
+ posthog.opt_in_capturing();
+ } else {
+ posthog.opt_out_capturing();
+ }
+ }
};
// Add one pattern from the WindowPicker. Reuses the MultiSelect change
diff --git a/apps/screenpipe-app-tauri/lib/analytics-id.ts b/apps/screenpipe-app-tauri/lib/analytics-id.ts
index 9260327d4..608a3398d 100644
--- a/apps/screenpipe-app-tauri/lib/analytics-id.ts
+++ b/apps/screenpipe-app-tauri/lib/analytics-id.ts
@@ -37,3 +37,28 @@ export function cacheAnalyticsId(id: string | undefined | null): void {
// best-effort and must never break app boot.
}
}
+
+// Same pattern for the analytics opt-out preference: cache it in localStorage
+// so providers.tsx can read it SYNCHRONOUSLY on boot and sync PostHog
+// opt-in/out right after init — before the async settings IPC resolves.
+export const ANALYTICS_ENABLED_KEY = "screenpipe_analytics_enabled";
+
+export function readCachedAnalyticsEnabled(): boolean | undefined {
+ if (typeof window === "undefined") return undefined;
+ try {
+ const raw = localStorage.getItem(ANALYTICS_ENABLED_KEY);
+ if (raw === null) return undefined;
+ return raw === "true";
+ } catch {
+ return undefined;
+ }
+}
+
+export function cacheAnalyticsEnabled(enabled: boolean): void {
+ if (typeof window === "undefined") return;
+ try {
+ localStorage.setItem(ANALYTICS_ENABLED_KEY, String(enabled));
+ } catch {
+ // Best-effort — must never break app boot.
+ }
+}
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
index 9f5b85feb..f3bf38763 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
@@ -10,7 +10,7 @@ import { Store } from "@tauri-apps/plugin-store";
import { emit, listen } from "@tauri-apps/api/event";
import React, { createContext, useContext, useEffect, useRef, useState } from "react";
import posthog from "posthog-js";
-import { cacheAnalyticsId } from "@/lib/analytics-id";
+import { cacheAnalyticsId, cacheAnalyticsEnabled } from "@/lib/analytics-id";
import { User } from "../utils/tauri";
import { SettingsStore } from "../utils/tauri";
import { installAuthInterceptor, stripSessionToken } from "../auth-guard";
@@ -1337,6 +1337,10 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
// person instead of a fresh per-webview anonymous id. See lib/analytics-id.
cacheAnalyticsId(settings.analyticsId);
+ // Cache the analytics opt-out preference so providers.tsx can sync
+ // PostHog opt-in/out on the next boot. See lib/analytics-id.
+ cacheAnalyticsEnabled(settings.analyticsEnabled);
+
const clerkId = settings.user?.clerk_id || undefined;
const distinctId = clerkId || settings.analyticsId;