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(analytics): count machines, not process starts, for bare-CLI runs (#4175)

Louis Beaumont9a75170abaa9
diff --git a/crates/screenpipe-engine/src/analytics.rs b/crates/screenpipe-engine/src/analytics.rs
index e3aed186a..7820c33da 100644
--- a/crates/screenpipe-engine/src/analytics.rs
+++ b/crates/screenpipe-engine/src/analytics.rs
@@ -25,10 +25,9 @@ pub struct Analytics {
impl Analytics {
fn new() -> Self {
- // Try env-provided analytics/support IDs first, then fall back to a
- // random UUID for standalone CLI usage.
- let distinct_id = TelemetryContext::distinct_id_from_env()
- .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
+ // Launcher-provided id when present, else the stable per-machine id —
+ // a fresh UUID per process start counted each run as a new user.
+ let distinct_id = TelemetryContext::distinct_id();
debug!("Analytics initialized with distinct_id: {}", distinct_id);
diff --git a/crates/screenpipe-engine/src/resource_monitor.rs b/crates/screenpipe-engine/src/resource_monitor.rs
index 005c0a10c..e9638234e 100644
--- a/crates/screenpipe-engine/src/resource_monitor.rs
+++ b/crates/screenpipe-engine/src/resource_monitor.rs
@@ -274,9 +274,9 @@ impl ResourceMonitor {
debug!("Telemetry disabled, will not send performance data to PostHog");
}
- // Use env-provided analytics/support IDs or generate a random UUID.
- let distinct_id = TelemetryContext::distinct_id_from_env()
- .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
+ // Launcher-provided id when present, else the stable per-machine id —
+ // a fresh UUID per process start counted each run as a new user.
+ let distinct_id = TelemetryContext::distinct_id();
// Collect host info once (OS, CPU, GPU names) — never panics
let hw_info = HardwareInfo::collect();
diff --git a/crates/screenpipe-engine/src/telemetry_context.rs b/crates/screenpipe-engine/src/telemetry_context.rs
index 8dbb51c57..5aa3efe3b 100644
--- a/crates/screenpipe-engine/src/telemetry_context.rs
+++ b/crates/screenpipe-engine/src/telemetry_context.rs
@@ -74,6 +74,18 @@ impl TelemetryContext {
first_env(DISTINCT_ID_ENV_VARS)
}
+ /// Stable telemetry distinct_id.
+ ///
+ /// Prefers a launcher-provided id (the desktop app's analytics id, or an
+ /// enterprise support/telemetry id). For a bare `screenpipe` CLI run — none
+ /// of those set — it falls back to the persistent per-machine id instead of
+ /// a fresh random UUID. Minting a new UUID on every process start made each
+ /// invocation look like a brand-new user, badly inflating PostHog user
+ /// counts for the CLI / Linux / Windows populations.
+ pub fn distinct_id() -> String {
+ Self::distinct_id_from_env().unwrap_or_else(screenpipe_core::sync::get_or_create_machine_id)
+ }
+
pub fn is_empty(&self) -> bool {
self.pairs().is_empty()
}
@@ -227,6 +239,27 @@ mod tests {
});
}
+ #[test]
+ fn distinct_id_falls_back_to_stable_machine_id() {
+ with_env(&[], || {
+ // Bare-CLI run (no launcher id): must reuse the persistent
+ // per-machine id, so it's identical across calls — i.e. across
+ // process restarts — instead of a fresh UUID each time (which made
+ // every invocation look like a brand-new user).
+ let first = TelemetryContext::distinct_id();
+ let second = TelemetryContext::distinct_id();
+ assert!(!first.is_empty());
+ assert_eq!(first, second);
+ });
+ }
+
+ #[test]
+ fn distinct_id_prefers_launcher_id_over_machine_id() {
+ with_env(&[("SCREENPIPE_ANALYTICS_ID", "analytics-user")], || {
+ assert_eq!(TelemetryContext::distinct_id(), "analytics-user");
+ });
+ }
+
#[test]
fn posthog_properties_include_person_set_values() {
with_env(