amplitude

Crates.ioamplitude
lib.rsamplitude
version0.1.0
sourcesrc
created_at2024-03-16 04:02:39.842889
updated_at2024-03-16 04:02:39.842889
descriptionRust HTTP API wrapper for Amplitude
homepage
repository
max_upload_size
id1175389
size24,951
Hardhat Chad (HardhatChad)

documentation

README

Amplitude

API overview

First example

use amplitude::{Amp, Event};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let amp = Amp::from_env()?; // api key as env variable must be provided
    let mut event1 = Event::new();
    event1
    	.user_id("some user id")
    	.event_type("some event type")
    	.country("UK")
    	.android_id("slmsung-unlimited-hash")
    	.ip4(Some(Ipv4Addr::new(127, 0, 0, 5)))
    	.time(chrono::Utc::now());
    let event2 = Event::from_json(json!(
    	{
            "user_id": "some user id",
            "event_type": "lay on the beach",
            "android_id": "3gfhtey534-647"
        }
    ))?;
    let response = amp.send(vec![event1, event2])?;
    
    Ok(())
}

Second example

use amplitude::{Amp, Event};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut amp = Amp::from_env()?; // api key as env variable must be provided
    amp
    	.batch() // set batch url
    	.set_min_id_length(4); 
    let mut event = Event::new();
    event
    	.device_id("xxxx")
    	.event_type("register");
    
    #[derive(Serialize)]
    struct UserProperties {
        age: u8,
        gender: String,
        interests: Vec<String>,
    }
    let up = UserProperties {
        age: 25,
        gender: "female".to_string(),
        interests: vec!["football".to_string(), "hockey".to_string()],
    };
    
    event.user_properties(up); // set user properties
    // also instead of creating a struct, you may pass serde_json::Value
    event.event_properties(json!({
        "event_group": "children",
        "foods": [
            {
                "apples": 5
            }
        ]
    }));
    let response = amp.send(vec![event]).await?;
}
Commit count: 0

cargo fmt