statsig-wasm
๐ 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.
Table of Contents
โธป
๐ฆ Installation
It's a library crate, so add it to your project with:
cargo add statsig-wasmโธป
๐ง Setup
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.
โธป
๐งช Usage
Initialise the client
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.
Initialise synchronously
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();Session replay & autocapture
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.
โธป
๐ ๏ธ Requirements
A Wasm frontend: this is a
wasm-bindgencrate meant to compile towasm32and run in the browser โ it's not a native-target libraryThe Statsig JS client on the page: the CDN script from Setup has to be loaded, since the bindings call into the global
StatsignamespaceRust 1.85+: the crate is on the 2024 edition, so you'll want a recent toolchain
โธป
๐ Repo & Contributions
๐ ๏ธ Repo: https://github.com/dsplce-co/statsig-wasm ๐ฆ Crate: https://crates.io/crates/statsig-wasm
PRs welcome, feel free to contribute
โธป
๐ License
MIT or Apache-2.0, at your option.