Rust experiments installation

  1. Install PostHog Rust SDK

    Required

    Install the posthog-rs crate by adding it to your Cargo.toml.

    Cargo.toml
    [dependencies]
    posthog-rs = "0.3.5"

    Next, set up the client with your PostHog project key.

    Rust
    let client = posthog_rs::client(env!("<ph_project_api_key>"));

    Blocking client

    Our Rust SDK supports both blocking and async clients. The async client is the default and is recommended for most use cases.

    If you need to use a synchronous client instead – like we do in our CLI –, you can opt into it by disabling the asynchronous feature on your Cargo.toml file.

    toml
    [dependencies]
    posthog-rs = { version = "0.3.5", default-features = false }

    In blocking mode, calls to capture and related methods will block until the PostHog event capture API returns – generally this is on the order of tens of milliseconds, but you may want to thread::spawn a background thread when you send an event.

  2. Capture conversion event

    Required

    Once PostHog is initialized, you should be able to capture events. For your experiment to be meaningful, we need to capture an event that we want to measure, such as a conversion event.

    For this tutorial, let's report a conversion event when a user clicks a CTA. In server SDKs like Rust, this can be called when a specific route is visited or reported from your client side app.

    Rust
    use posthog_rs::Event;
    let event = Event::new("cta clicked", "user_distinct_id");
    client.capture(event).await.unwrap();
  3. Validate PostHog events

    Checkpoint
    Confirm events are being sent to PostHog

    Before proceeding, let's make sure events are being captured and sent to PostHog. You should see cta clicked events appear in the Activity feed.

    Check for events in PostHog

  4. Create an experiment

    Required

    Go to the Experiments tab in the PostHog app and click on the New experiment button in the top right.

    Create experiment

    For this tutorial, let's create a new experiment using simplified test values:

    • Name: "Test experiment"
    • Description: "This is a test experiment"
    • Feature flag key: "test-experiment-ff-key"
    • Experiment type: "Feature flag"
    • Variants: "control" and "test"
    • Participant type: "Users"

    Then click Save as draft.

  5. Add primary metric and launch

    Required

    Scroll down to the Primary metrics section and click + Add primary metric.

    Choose Single-use and select Type > Mean.

    Then search for the event cta clicked under Metric and click Save.

    Add primary metric

    By default, experiments are exposed to 100% of users. You can customize release conditions to expose the experiment to a subset of users.

    For this tutorial, we'll ship the experiment to all users and click Launch in the top right.

  6. Call feature flag

    Required

    Use the PostHog SDK to call the experiment flag and update how your page renders based on the assigned variant.

    Rust
    use posthog_rs::FlagValue;
    match client.get_feature_flag(
    "your-experiment-key".to_string(),
    "user_distinct_id".to_string(),
    None, None, None
    ).await.unwrap() {
    Some(FlagValue::String(variant)) if variant == "test" => {
    // Show test variant UI
    }
    _ => {
    // Show control variant UI
    }
    }

    Now when a user triggers a cta clicked event, PostHog automatically assigns the user to a variant and records an experiment exposure.

    By default, users are split equally between variants. If you want to assign specific users to a specific variant, see more about distribution and release conditions.

  7. Validate feature flag calls

    Checkpoint

    Make sure exposures and feature flag calls are being sent to PostHog. You should see $feature_flag_called events appear in the Activity feed.

    Check for events in PostHog

  8. Evaluate experiment results

    Recommended

    As you capture more cta clicked events, more exposures will populate the primary metrics in your experiment.

    Evaluate experiment metrics

    With enough data, you can analyze the experiment and its variants by:

    • Conversion rates
    • Statistical significance
    • Credible intervals
    • Chance to win %
    • Minimum detectable effect
    • And more

Community questions

Was this page useful?

Questions about this page? or post a community question.