Skip to content

arsenron/amplitude

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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?;
}