matomo-rs 0.1.0

Async client for the Matomo Reporting API, focused on data export and migration
Documentation
//! Stream `Live.getLastVisitsDetails` over a date range and print a few fields
//! per visit.
//!
//! Run with:
//!   MATOMO_URL=https://host/matomo/ MATOMO_TOKEN=... MATOMO_IDSITE=1 \
//!     cargo run --example export

use std::env;

use futures::StreamExt;
use matomo::reqwest::MatomoClient;
use matomo::{Auth, DateRange, Limit, Period};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = env::var("MATOMO_URL")?;
    let token = env::var("MATOMO_TOKEN")?;
    let id_site: u32 = env::var("MATOMO_IDSITE")?.parse()?;

    let client = MatomoClient::builder()
        .base_url(url)
        .auth(Auth::token(token))
        .build()?;

    // Preflight runs lazily on the first call below.
    let period = Period::Range(DateRange::LastN(7));
    let page_size = Limit::count(50).expect("50 is non-zero");

    let mut stream = client.live().stream(id_site, period, page_size, None)?;

    while let Some(item) = stream.next().await {
        let visit = item?;
        println!(
            "visit {} type={:?} country={:?} actions={}",
            visit.id_visit, visit.visitor_type, visit.country, visit.actions
        );
    }

    Ok(())
}