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::{default_api_endpoint, PostHogConfig};
14
15pub trait PostHogExt<R: Runtime> {
16    fn posthog(&self) -> &PostHogClientWrapper;
17}
18
19impl<R: Runtime, T: Manager<R>> PostHogExt<R> for T {
20    fn posthog(&self) -> &PostHogClientWrapper {
21        self.state::<PostHogClientWrapper>().inner()
22    }
23}
24
25/// Initialize PostHog plugin with configuration
26pub fn init<R: Runtime>(config: PostHogConfig) -> TauriPlugin<R> {
27    Builder::new("posthog")
28        .invoke_handler(tauri::generate_handler![
29            commands::capture,
30            commands::identify,
31            commands::alias,
32            commands::reset,
33            commands::get_distinct_id,
34            commands::get_device_id,
35            commands::capture_batch,
36        ])
37        .setup(move |app, _api| {
38            tauri::async_runtime::block_on(async {
39                let client = PostHogClientWrapper::new(config).await?;
40                app.manage(client);
41                Ok(())
42            })
43        })
44        .build()
45}