mod flush;
mod id;
mod queue;
pub use self::track_command_with_tier as track_command_tier;
pub use flush::{flush_analytics, force_flush_sync};
pub use id::{generate_anon_id, generate_file_hash};
pub use queue::{track_event, AnalyticsEvent};
use std::sync::atomic::{AtomicBool, Ordering};
static TELEMETRY_ENABLED: AtomicBool = AtomicBool::new(true);
pub fn is_telemetry_enabled() -> bool {
TELEMETRY_ENABLED.load(Ordering::Relaxed)
}
pub fn init_analytics() {
if let Ok(val) = std::env::var("MEMVID_TELEMETRY") {
if val == "0" || val.to_lowercase() == "false" {
TELEMETRY_ENABLED.store(false, Ordering::Relaxed);
return;
}
}
flush::start_background_flush();
}
pub fn track_command(
file_path: Option<&str>,
command: &str,
success: bool,
file_created: bool,
file_opened: bool,
) {
track_command_with_tier(
file_path,
command,
success,
file_created,
file_opened,
"free",
)
}
pub fn track_command_with_tier(
file_path: Option<&str>,
command: &str,
success: bool,
file_created: bool,
file_opened: bool,
user_tier: &str,
) {
if !is_telemetry_enabled() {
return;
}
let anon_id = generate_anon_id(file_path);
let file_hash = file_path
.map(|p| generate_file_hash(p))
.unwrap_or_else(|| "none".to_string());
let event = AnalyticsEvent {
anon_id,
file_hash,
client: "cli".to_string(),
command: command.to_string(),
success,
timestamp: chrono::Utc::now().to_rfc3339(),
file_created,
file_opened,
user_tier: user_tier.to_string(),
};
track_event(event);
}