tauri_plugin_posthog/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Manager, Runtime,
4};
5
6mod client;
7mod commands;
8mod error;
9mod models;
10
11use client::PostHogClientWrapper;
12pub use error::{Error, Result};
13pub use models::{
14    default_api_host, AliasRequest, CaptureRequest, IdentifyRequest, PostHogConfig, PostHogOptions,
15};
16
17pub trait PostHogExt<R: Runtime> {
18    fn posthog(&self) -> &PostHogClientWrapper;
19}
20
21impl<R: Runtime, T: Manager<R>> PostHogExt<R> for T {
22    fn posthog(&self) -> &PostHogClientWrapper {
23        self.state::<PostHogClientWrapper>().inner()
24    }
25}
26
27/// Initialize PostHog plugin with configuration
28pub fn init<R: Runtime>(config: PostHogConfig) -> TauriPlugin<R, ()> {
29    Builder::<R>::new("posthog")
30        .invoke_handler(tauri::generate_handler![
31            commands::capture,
32            commands::identify,
33            commands::alias,
34            commands::reset,
35            commands::get_distinct_id,
36            commands::get_config,
37        ])
38        .setup(move |app, _api| {
39            let app_handle = app.clone();
40            tauri::async_runtime::spawn(async move {
41                match PostHogClientWrapper::new(config).await {
42                    Ok(client) => {
43                        app_handle.manage(client);
44                    }
45                    Err(e) => {
46                        eprintln!("Failed to initialize PostHog client: {}", e);
47                    }
48                }
49            });
50            Ok(())
51        })
52        .build()
53}