memvid_cli/analytics/
mod.rs1mod flush;
10mod id;
11mod queue;
12
13pub use self::track_command_with_tier as track_command_tier;
14pub use flush::{flush_analytics, force_flush_sync};
15pub use id::{generate_anon_id, generate_file_hash};
16pub use queue::{track_event, AnalyticsEvent};
17
18use std::sync::atomic::{AtomicBool, Ordering};
19
20static TELEMETRY_ENABLED: AtomicBool = AtomicBool::new(true);
22
23pub fn is_telemetry_enabled() -> bool {
26 TELEMETRY_ENABLED.load(Ordering::Relaxed)
27}
28
29pub fn init_analytics() {
32 if let Ok(val) = std::env::var("MEMVID_TELEMETRY") {
34 if val == "0" || val.to_lowercase() == "false" {
35 TELEMETRY_ENABLED.store(false, Ordering::Relaxed);
36 return;
37 }
38 }
39
40 flush::start_background_flush();
42}
43
44pub fn track_command(
46 file_path: Option<&str>,
47 command: &str,
48 success: bool,
49 file_created: bool,
50 file_opened: bool,
51) {
52 track_command_with_tier(
53 file_path,
54 command,
55 success,
56 file_created,
57 file_opened,
58 "free",
59 )
60}
61
62pub fn track_command_with_tier(
64 file_path: Option<&str>,
65 command: &str,
66 success: bool,
67 file_created: bool,
68 file_opened: bool,
69 user_tier: &str,
70) {
71 if !is_telemetry_enabled() {
72 return;
73 }
74
75 let anon_id = generate_anon_id(file_path);
76 let file_hash = file_path
77 .map(|p| generate_file_hash(p))
78 .unwrap_or_else(|| "none".to_string());
79
80 let event = AnalyticsEvent {
81 anon_id,
82 file_hash,
83 client: "cli".to_string(),
84 command: command.to_string(),
85 success,
86 timestamp: chrono::Utc::now().to_rfc3339(),
87 file_created,
88 file_opened,
89 user_tier: user_tier.to_string(),
90 };
91
92 track_event(event);
94}