From 6784c60f0002a39da048f162b4b49b2d8f8e107c Mon Sep 17 00:00:00 2001 From: Gabriel Gordon-Hall Date: Wed, 9 Jul 2025 17:30:56 +0100 Subject: [PATCH] chore: track user device (#111) * Implementation completed successfully! I've added device information capture using the `os_info` crate to the analytics service. Here's what was implemented: 1. **Added `os_info` crate dependency** to `backend/Cargo.toml` 2. **Created `get_device_info()` function** in `backend/src/services/analytics.rs:172-181` that captures: - Operating system type - OS version - Architecture - Bitness 3. **Integrated device info** into the `track_event()` method at `backend/src/services/analytics.rs:79` so every analytics event now includes device information in the properties The device information is now automatically included with every analytics event sent to PostHog, providing valuable context about the user's environment. * fmt --- backend/Cargo.toml | 1 + backend/src/services/analytics.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/backend/Cargo.toml b/backend/Cargo.toml index c77f6774..df4b4454 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -42,6 +42,7 @@ schemars = "0.8" regex = "1.11.1" notify-rust = "4.11" octocrab = "0.44" +os_info = "3.12.0" sentry = { version = "0.41.0", features = ["anyhow", "backtrace", "panic", "debug-images"] } sentry-tower = "0.41.0" sentry-tracing = { version = "0.41.0", features = ["backtrace"] } diff --git a/backend/src/services/analytics.rs b/backend/src/services/analytics.rs index bf617c09..e68caa62 100644 --- a/backend/src/services/analytics.rs +++ b/backend/src/services/analytics.rs @@ -4,6 +4,7 @@ use std::{ time::Duration, }; +use os_info; use serde_json::{json, Value}; #[derive(Debug, Clone)] @@ -75,6 +76,7 @@ impl AnalyticsService { json!(chrono::Utc::now().to_rfc3339()), ); props.insert("version".to_string(), json!(env!("CARGO_PKG_VERSION"))); + props.insert("device".to_string(), get_device_info()); } payload["properties"] = event_properties; } @@ -167,6 +169,17 @@ pub fn generate_user_id() -> String { format!("npm_user_{:016x}", hasher.finish()) } +fn get_device_info() -> Value { + let info = os_info::get(); + + json!({ + "os_type": info.os_type().to_string(), + "os_version": info.version().to_string(), + "architecture": info.architecture().unwrap_or("unknown").to_string(), + "bitness": info.bitness().to_string(), + }) +} + #[cfg(test)] mod tests { use super::*;