|
| 1 | +//! Analytics module for UltraLog using PostHog. |
| 2 | +//! |
| 3 | +//! This module provides anonymous usage analytics to help improve UltraLog. |
| 4 | +//! All data is anonymous - we only track feature usage, not personal information. |
| 5 | +
|
| 6 | +use posthog_rs::Event; |
| 7 | +use std::sync::OnceLock; |
| 8 | +use uuid::Uuid; |
| 9 | + |
| 10 | +/// PostHog API key for UltraLog analytics |
| 11 | +const POSTHOG_API_KEY: &str = "phc_jrZkZhkhoHXknLz7djnuBR8s4tl9mZnR00UAWVl2GHO"; |
| 12 | + |
| 13 | +/// Application version for tracking |
| 14 | +const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 15 | + |
| 16 | +/// Global distinct ID for this session |
| 17 | +static DISTINCT_ID: OnceLock<String> = OnceLock::new(); |
| 18 | + |
| 19 | +/// Flag to track if global client is initialized |
| 20 | +static INITIALIZED: OnceLock<bool> = OnceLock::new(); |
| 21 | + |
| 22 | +/// Get or generate the session's distinct ID |
| 23 | +fn get_distinct_id() -> &'static str { |
| 24 | + DISTINCT_ID.get_or_init(|| Uuid::new_v4().to_string()) |
| 25 | +} |
| 26 | + |
| 27 | +/// Initialize the global PostHog client (call once at startup) |
| 28 | +fn ensure_initialized() { |
| 29 | + INITIALIZED.get_or_init(|| { |
| 30 | + // Initialize the global PostHog client using builder pattern |
| 31 | + if let Ok(options) = posthog_rs::ClientOptionsBuilder::default() |
| 32 | + .api_key(POSTHOG_API_KEY.to_string()) |
| 33 | + .build() |
| 34 | + { |
| 35 | + let _ = posthog_rs::init_global(options); |
| 36 | + } |
| 37 | + true |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/// Get the current platform as a string |
| 42 | +fn get_platform() -> &'static str { |
| 43 | + #[cfg(target_os = "windows")] |
| 44 | + return "windows"; |
| 45 | + |
| 46 | + #[cfg(target_os = "macos")] |
| 47 | + return "macos"; |
| 48 | + |
| 49 | + #[cfg(target_os = "linux")] |
| 50 | + return "linux"; |
| 51 | + |
| 52 | + #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] |
| 53 | + return "unknown"; |
| 54 | +} |
| 55 | + |
| 56 | +/// Create a base event with common properties |
| 57 | +fn create_event(event_name: &str) -> Event { |
| 58 | + let mut event = Event::new(event_name, get_distinct_id()); |
| 59 | + |
| 60 | + // Add common properties |
| 61 | + let _ = event.insert_prop("app_version", APP_VERSION); |
| 62 | + let _ = event.insert_prop("platform", get_platform()); |
| 63 | + |
| 64 | + event |
| 65 | +} |
| 66 | + |
| 67 | +/// Capture an event using the global client (fire and forget - errors are silently ignored) |
| 68 | +fn capture_event(event: Event) { |
| 69 | + ensure_initialized(); |
| 70 | + |
| 71 | + // Spawn in background thread to avoid blocking UI |
| 72 | + std::thread::spawn(move || { |
| 73 | + let _ = posthog_rs::capture(event); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +// ============================================================================ |
| 78 | +// Public Analytics Functions |
| 79 | +// ============================================================================ |
| 80 | + |
| 81 | +/// Track application startup |
| 82 | +pub fn track_app_started() { |
| 83 | + let event = create_event("app_started"); |
| 84 | + capture_event(event); |
| 85 | +} |
| 86 | + |
| 87 | +/// Track when a log file is loaded |
| 88 | +pub fn track_file_loaded(ecu_type: &str, file_size_bytes: u64) { |
| 89 | + let mut event = create_event("file_loaded"); |
| 90 | + |
| 91 | + let _ = event.insert_prop("ecu_type", ecu_type); |
| 92 | + let _ = event.insert_prop("file_size_kb", file_size_bytes / 1024); |
| 93 | + |
| 94 | + capture_event(event); |
| 95 | +} |
| 96 | + |
| 97 | +/// Track when a channel is selected |
| 98 | +pub fn track_channel_selected(channel_count: usize) { |
| 99 | + let mut event = create_event("channel_selected"); |
| 100 | + |
| 101 | + let _ = event.insert_prop("total_channels", channel_count); |
| 102 | + |
| 103 | + capture_event(event); |
| 104 | +} |
| 105 | + |
| 106 | +/// Track chart export (PNG or PDF) |
| 107 | +pub fn track_export(format: &str) { |
| 108 | + let mut event = create_event("chart_exported"); |
| 109 | + |
| 110 | + let _ = event.insert_prop("format", format); |
| 111 | + |
| 112 | + capture_event(event); |
| 113 | +} |
| 114 | + |
| 115 | +/// Track tool/view switch |
| 116 | +pub fn track_tool_switched(tool_name: &str) { |
| 117 | + let mut event = create_event("tool_switched"); |
| 118 | + |
| 119 | + let _ = event.insert_prop("tool", tool_name); |
| 120 | + |
| 121 | + capture_event(event); |
| 122 | +} |
| 123 | + |
| 124 | +/// Track playback usage |
| 125 | +pub fn track_playback_started(speed: f64) { |
| 126 | + let mut event = create_event("playback_started"); |
| 127 | + |
| 128 | + let _ = event.insert_prop("speed", speed); |
| 129 | + |
| 130 | + capture_event(event); |
| 131 | +} |
| 132 | + |
| 133 | +/// Track unit preference changes |
| 134 | +#[allow(dead_code)] |
| 135 | +pub fn track_unit_changed(unit_category: &str, new_unit: &str) { |
| 136 | + let mut event = create_event("unit_changed"); |
| 137 | + |
| 138 | + let _ = event.insert_prop("category", unit_category); |
| 139 | + let _ = event.insert_prop("new_unit", new_unit); |
| 140 | + |
| 141 | + capture_event(event); |
| 142 | +} |
| 143 | + |
| 144 | +/// Track update check |
| 145 | +pub fn track_update_checked(update_available: bool) { |
| 146 | + let mut event = create_event("update_checked"); |
| 147 | + |
| 148 | + let _ = event.insert_prop("update_available", update_available); |
| 149 | + |
| 150 | + capture_event(event); |
| 151 | +} |
| 152 | + |
| 153 | +/// Track colorblind mode toggle |
| 154 | +pub fn track_colorblind_mode_toggled(enabled: bool) { |
| 155 | + let mut event = create_event("colorblind_mode_toggled"); |
| 156 | + |
| 157 | + let _ = event.insert_prop("enabled", enabled); |
| 158 | + |
| 159 | + capture_event(event); |
| 160 | +} |
| 161 | + |
| 162 | +/// Track file format detection errors (helps prioritize new format support) |
| 163 | +#[allow(dead_code)] |
| 164 | +pub fn track_file_format_error(error_type: &str) { |
| 165 | + let mut event = create_event("file_format_error"); |
| 166 | + |
| 167 | + let _ = event.insert_prop("error_type", error_type); |
| 168 | + |
| 169 | + capture_event(event); |
| 170 | +} |
0 commit comments