Skip to content

Commit

Permalink
chore: Add Mixpanel event tracking and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
miguoliang committed Jun 23, 2024
1 parent c8fcfd8 commit 40d7163
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
30 changes: 20 additions & 10 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::Utc;
use reqwest::Client;
use serde::Serialize;
use std::{collections::HashMap, process::exit};
use std::collections::HashMap;
use uuid::Uuid;

#[derive(Serialize, Debug)]
Expand All @@ -17,7 +17,7 @@ impl Event {
properties.insert("$insert_id".to_string(), Uuid::new_v4().to_string());
Event {
event: name.to_string(),
properties: HashMap::new(),
properties,
}
}

Expand All @@ -40,15 +40,25 @@ impl MixpanelClient {
let client = Client::new();
let url = "https://api.mixpanel.com/track";

let response = client
.post(url)
.basic_auth(self.token.to_string(), Option::from(""))
.json(&event)
.send()
.await;
event.add_property("token", &self.token);
if let Some(distinct_id) = &self.distinct_id {
event.add_property("distinct_id", distinct_id);
}

let response = client.post(url).json(&[event]).send().await;

if let Err(err) = response {
eprintln!("Failed to send event: {}", err);
match response {
Ok(resp) => {
if resp.status().is_success() {
match resp.text().await {
Ok(body) => println!("Event sent successfully. Response body: {:?}", body),
Err(err) => eprintln!("Failed to read response body: {}", err),
}
} else {
eprintln!("Failed to send event: HTTP {}", resp.status());
}
}
Err(err) => eprintln!("Failed to send event: {}", err),
}
}
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ async fn main() {
);
println!("");

let token = std::env::var("MIXPANEL_TOKEN").expect("MIXPANEL_TOKEN is not set");
// let token = std::env::var("MIXPANEL_TOKEN").expect("MIXPANEL_TOKEN is not set");
let token = String::from("d83029f8b9507a4f672fe96cc3fe9b2c");
let distinct_id = std::env::var("MIXPANEL_DISTINCT_ID").ok();
let mixpanel_client = create_mixpanel_client(token, distinct_id).await;
send_event(mixpanel_client.clone(), "Start", HashMap::new()).await;
Expand Down

0 comments on commit 40d7163

Please sign in to comment.