Skip to main content

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, Window};
16
17/// Options for customizing the analytics backend the plugin talks to.
18///
19/// The plugin is backend-agnostic: there is no implicit host resolution. Set
20/// `host` (and optionally `api_path`, `app_key_header` and `sdk_name`) to point
21/// the plugin at your analytics backend. When `host` is unset, tracking is
22/// disabled.
23#[derive(Default, Debug, Clone)]
24pub struct InitOptions {
25    /// Base URL of the analytics backend (e.g. `https://analytics.myapp.com`).
26    ///
27    /// Required to enable tracking. The plugin sends events to
28    /// `{host}{api_path}`.
29    pub host: Option<String>,
30    /// How often to flush queued events to the backend. Defaults to 60s in
31    /// release builds and 2s in debug builds.
32    pub flush_interval: Option<Duration>,
33    /// Path appended to `host` to form the ingest URL. Defaults to
34    /// `/v1/events`.
35    pub api_path: Option<String>,
36    /// Name of the HTTP header that carries the App Key. Defaults to
37    /// `App-Key`.
38    pub app_key_header: Option<String>,
39    /// Value reported in `systemProps.sdkVersion` on each event. Defaults to
40    /// `<crate-name>@<crate-version>`.
41    pub sdk_name: Option<String>,
42}
43
44/// Trait implemented by Tauri handlers.
45pub trait EventTracker {
46    /// Enqueues an event to be sent to the backend.
47    fn track_event(&self, name: &str, props: Option<Value>) -> Result<(), String>;
48
49    /// Flushes the event queue, blocking the current thread.
50    fn flush_events_blocking(&self);
51}
52
53macro_rules! impl_event_tracker {
54    ($($t:ty),*) => {
55        $(
56            impl EventTracker for $t {
57                fn track_event(&self, name: &str, props: Option<Value>) -> Result<(), String> {
58                    let client = self.state::<Arc<TelemetryClient>>();
59                    client.track_event(name, props)
60                }
61
62                fn flush_events_blocking(&self) {
63                    let client = self.state::<Arc<TelemetryClient>>();
64                    client.flush_blocking()
65                }
66            }
67        )*
68    };
69}
70
71impl_event_tracker!(App, AppHandle, Window);