tauri_plugin_telemetry/lib.rs
1mod builder;
2mod client;
3mod commands;
4mod config;
5mod dispatcher;
6mod session;
7mod sys;
8
9use std::sync::Arc;
10use std::time::Duration;
11
12pub use builder::{Builder, PanicHook};
13use client::TelemetryClient;
14use serde_json::Value;
15use tauri::{App, AppHandle, Manager, Runtime, Window};
16
17/// Shorthand for `Builder::new(app_key).build()`.
18///
19/// For full control over `InitOptions` or a custom panic hook, use
20/// [`Builder`] directly.
21pub fn init<R: Runtime>(app_key: &str) -> tauri::plugin::TauriPlugin<R> {
22 Builder::new(app_key).build()
23}
24
25/// Options for customizing the analytics backend the plugin talks to.
26///
27/// The plugin is backend-agnostic: there is no implicit host resolution. Set
28/// `host` (and optionally `api_path`, `app_key_header` and `sdk_name`) to point
29/// the plugin at your analytics backend. When `host` is unset, tracking is
30/// disabled.
31#[derive(Default, Debug, Clone)]
32pub struct InitOptions {
33 /// Base URL of the analytics backend (e.g. `https://analytics.myapp.com`).
34 ///
35 /// Required to enable tracking. The plugin sends events to
36 /// `{host}{api_path}`.
37 pub host: Option<String>,
38 /// How often to flush queued events to the backend. Defaults to 60s in
39 /// release builds and 2s in debug builds.
40 pub flush_interval: Option<Duration>,
41 /// Path appended to `host` to form the ingest URL. Defaults to
42 /// `/v1/events`.
43 pub api_path: Option<String>,
44 /// Name of the HTTP header that carries the App Key. Defaults to
45 /// `App-Key`.
46 pub app_key_header: Option<String>,
47 /// Value reported in `systemProps.sdkVersion` on each event. Defaults to
48 /// `<crate-name>@<crate-version>`.
49 pub sdk_name: Option<String>,
50}
51
52/// Trait implemented by Tauri handlers.
53pub trait EventTracker {
54 /// Enqueues an event to be sent to the backend.
55 fn track_event(&self, name: &str, props: Option<Value>) -> Result<(), String>;
56
57 /// Flushes the event queue, blocking the current thread.
58 fn flush_events_blocking(&self);
59}
60
61macro_rules! impl_event_tracker {
62 ($($t:ty),*) => {
63 $(
64 impl EventTracker for $t {
65 fn track_event(&self, name: &str, props: Option<Value>) -> Result<(), String> {
66 let client = self.state::<Arc<TelemetryClient>>();
67 client.track_event(name, props)
68 }
69
70 fn flush_events_blocking(&self) {
71 let client = self.state::<Arc<TelemetryClient>>();
72 client.flush_blocking()
73 }
74 }
75 )*
76 };
77}
78
79impl_event_tracker!(App, AppHandle, Window);