Skip to content

Laravel Rust ​

The official simplestats-laravel-rust-client crate brings SimpleStats to apps built on Laravel Rust. It tracks the same data as the Laravel client package: visitors with their UTM parameters, referrer and entry page, registrations, logins, payments, page views, custom events and custom properties. Everything lands on the same dashboard, so a Laravel app on PHP and one on Rust can live side by side in the same team.

TIP

This page covers the setup. Every option, the payment trait and the differences to the PHP client are explained in the README on GitHub.

Requirements ​

  • Laravel Rust 0.1
  • Rust 1.99 or later
  • A queue worker (page views stay off on the sync queue)

Installation ​

Add the crate to your app (the crate is called simplestats in your code):

bash
cargo add simplestats-laravel-rust-client

Register the service provider in src/app.rs and tell it which model your users are:

rust
// src/app.rs
Application::configure()
    .with_providers(|providers| {
        providers.add(simplestats::SimplestatsServiceProvider::new().track_users::<User>());
    })

Implement the TrackablePerson trait on that model. Every method has a default, so one line is enough:

rust
// src/models/user.rs
impl simplestats::TrackablePerson for User {}

Add the API token of your project to your .env:

bash
SIMPLESTATS_API_TOKEN=your-project-token

From the next request on, visitors, page views, registrations and session logins are tracked. The API calls run through your queue, so keep a worker running:

bash
laravel queue:work

Check Your Setup ​

bash
laravel simplestats:status

The command shows whether your token works, the API is reachable and page views are on. Without the Laravel CLI, run cargo run -- simplestats:status.

Payments ​

Laravel Rust has no Cashier, so there are no payment provider presets. Implement the TrackablePayment trait on the model where you store your payments and register it with .track_payments::<Payment>(). That works with any provider. Amounts are in cents:

rust
impl simplestats::TrackablePayment for Payment {
    async fn tracking_person(&self) -> laravel::Result<simplestats::Person> {
        let user = User::find_or_fail(self.user_id).await?;
        Ok(simplestats::Person::from(&user))
    }

    fn tracking_gross(&self) -> f64 { self.gross as f64 }
    fn tracking_net(&self) -> f64 { self.net as f64 }
    fn tracking_currency(&self) -> String { self.currency.clone() }
}

Subscriptions (for MRR and ARR), guest checkouts and payments that should wait for a completed status are covered in the README.

Logins, Custom Events and Properties ​

Session logins are detected automatically. Logins through an API token, custom events and custom properties are one call each:

rust
simplestats::track_login(&user).await;
simplestats::track_custom_event(order.id.to_string(), "order_shipped", &user).await;
simplestats::track_custom_properties([("plan", "pro")], &user).await;

Good to Know ​

  • Pages behind a login: the crate cannot see which routes require a login, so add your logged-in area to except (e.g. SIMPLESTATS_EXCEPT=telescope*,horizon*,pulse*,admin*,dashboard*) to keep app usage out of your page views.
  • Bot detection: a built-in heuristic filters bots by default. For the same precision as the PHP client, enable the optional bot-detection feature of the crate (see the README for its licence).
  • Headless and SPA setups: set SIMPLESTATS_TRACKING_STORAGE=cache and SIMPLESTATS_MIDDLEWARE_GROUPS=api, as described in Headless, SPA & Stateless.
  • Static caching and marketing sites: enable the beacon with SIMPLESTATS_BEACON_ENABLED=true.
  • Beta framework: Laravel Rust is still a 0.x beta. The crate follows semantic versioning, and the release notes tell you when an update needs a change in your app.