#message #segment #user #track #client-send #batcher

bin+lib analytics

Segment analytics client for Rust https://segment.com/docs/libraries/rust

2 unstable releases

0.2.0 Jun 28, 2019
0.1.0 Jun 24, 2019

#388 in HTTP client

Download history 123/week @ 2023-12-09 103/week @ 2023-12-16 203/week @ 2023-12-30 398/week @ 2024-01-06 444/week @ 2024-01-13 273/week @ 2024-01-20 308/week @ 2024-01-27 540/week @ 2024-02-03 198/week @ 2024-02-10 120/week @ 2024-02-17 93/week @ 2024-02-24 42/week @ 2024-03-02 57/week @ 2024-03-09 80/week @ 2024-03-16 204/week @ 2024-03-23

411 downloads per month

MIT license

33KB
508 lines

Analytics Rust   Build Status Latest Version

Analytics Rust is a Segment analytics client for Rust. For additional documentation visit https://segment.com/docs/sources/#server.


[dependencies]
analytics = "0.1"

Example usage(s)

use analytics::http::HttpClient;
use analytics::client::Client;
use analytics::message::{BatchMessage, Track, User};
use analytics::batcher::Batcher;
use serde_json::json;

fn main() {
    let write_key = "YOUR_WRITE_KEY";

    let client = HttpClient::default();
    let mut batcher = Batcher::new(None);

    // Pretend this is reading off of a queue, a file, or some other data
    // source.
    for i in 0..100 {
        let msg = BatchMessage::Track(Track {
            user: User::UserId { user_id: format!("user-{}", i) },
            event: "Example Event".to_owned(),
            properties: json!({
                "foo": format!("bar-{}", i),
            }),
            ..Default::default()
        });

        // An error here indicates a message is too large. In real life, you
        // would probably want to put this message in a deadletter queue or some
        // equivalent.
        if let Some(msg) = batcher.push(msg).unwrap() {
            client.send(write_key, &batcher.into_message()).unwrap();

            batcher = Batcher::new(None);
            batcher.push(msg).unwrap(); // Same error condition as above.
        }
    }
}

or when you want to do struct to struct transformations

use analytics::http::HttpClient;
use analytics::client::Client;
use analytics::message::{Track, Message, User};
use serde_json::json;

fn main() {
    let write_key = "YOUR_WRITE_KEY";

    let client = HttpClient::default();
    client.send(write_key, &Message::Track(Track {
        user: User::UserId { user_id: "some_user_id".to_owned() },
        event: "Example Event".to_owned(),
        properties: json!({
            "some property": "some value",
            "some other property": "some other value",
        }),
        ..Default::default()
    })).expect("could not send to Segment");
}

License

Licensed under MIT license.

Dependencies

~21MB
~452K SLoC