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            // Use block_on to synchronously initialize the client before setup completes
40            let client =
41                tauri::async_runtime::block_on(async { PostHogClientWrapper::new(config).await })?;
42
43            app.manage(client);
44            Ok(())
45        })
46        .build()
47}