We're dsplce.co, check out our work on our website: dsplce.co 🖤
📈 Wasm bindings for the Statsig JavaScript (Web) SDK.
statsig-wasm lets you drive Statsig — initialise the client, run session replay, run autocapture — straight from your Rust/Wasm frontend, instead of dropping back into a patch of hand-written JS every time. You define your user in Rust, it gets serialised across the Wasm boundary for you, and the rest is just method calls on a typed StatsigClient.
Disclaimer: this project has no affiliation with the official Statsig project or trademark.
⸻
It's a library crate, so add it to your project with:
cargo add statsig-wasm⸻
These bindings call into the Statsig JS client that lives on the global Statsig namespace — so that script has to be on the page before your Wasm module runs. Add the following to your HTML <head>:
<script
src="https://cdn.jsdelivr.net/npm/@statsig/js-client@3/build/statsig-js-client+session-replay+web-analytics.min.js"
crossorigin="anonymous"
>
</script>That particular bundle ships session replay and web analytics (autocapture) baked in, which is what the run_statsig_session_replay and run_statsig_auto_capture bindings below reach for — so keep those in the URL if you intend to use them.
Bear in mind, if you're happy for Statsig to autoinitialise the client, just append ?apikey=<YOUR_CLIENT_API_KEY> to the script's src above and uninstall this crate 😉
If you're looking to initialise the client yourself, read on.
⸻
Build a StatsigClient from your client API key and a user, then kick off initialisation:
use statsig_wasm::{run_statsig_auto_capture, StatsigClient, StatsigUser};
let statsig = StatsigClient::new(
env!("STATSIG_API_KEY"),
StatsigUser {
user_id: user_id.get(),
},
)
.unwrap();
run_statsig_auto_capture(&statsig);
// `spawn_local` is native to Leptos, use your
// framework's equivalent to run the async method.
spawn_local(async move {
statsig.initialize().await;
});StatsigUser is serialised to the shape Statsig expects (your user_id lands as userID), so you stay in Rust and don't hand-roll the JS object.
If you're not in an async context — or just don't want to await — there's a synchronous initialiser that returns straight away:
statsig.initialize_sync();Both are one-liners that take the client you've already built. Call them before (or right alongside) initialisation:
use statsig_wasm::{run_statsig_auto_capture, run_statsig_session_replay};
run_statsig_session_replay(&statsig);
run_statsig_auto_capture(&statsig);Just remember they need the matching features in the JS bundle from Setup — the session-replay+web-analytics build covers both.
⸻
- A Wasm frontend: this is a
wasm-bindgencrate meant to compile towasm32and run in the browser — it's not a native-target library - The Statsig JS client on the page: the CDN script from Setup has to be loaded, since the bindings call into the global
Statsignamespace - Rust 1.85+: the crate is on the 2024 edition, so you'll want a recent toolchain
⸻
🛠️ Repo: https://github.com/dsplce-co/statsig-wasm
📦 Crate: https://crates.io/crates/statsig-wasm
PRs welcome, feel free to contribute
⸻
MIT or Apache-2.0, at your option.