Skip to content

Commit

Permalink
chore: Use Posthog instead of Mixpanel to collect stats on new projec…
Browse files Browse the repository at this point in the history
…ts creation (#227)
  • Loading branch information
garikbesson authored Oct 9, 2024
1 parent f2757c4 commit 1a795ee
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cargo-near/src/commands/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::process::Stdio;

use color_eyre::eyre::{ContextCompat, WrapErr};

use crate::mixpanel_tracking;
use crate::posthog_tracking;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = near_cli_rs::GlobalContext)]
Expand Down Expand Up @@ -54,7 +54,7 @@ impl NewContext {
}

let _detached_thread_handle = std::thread::Builder::new()
.spawn(mixpanel_tracking::track_usage)
.spawn(posthog_tracking::track_usage)
.unwrap();

let status = std::process::Command::new("git")
Expand Down
2 changes: 1 addition & 1 deletion cargo-near/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage};
pub mod commands;
pub mod types;

pub(crate) mod mixpanel_tracking;
pub(crate) mod posthog_tracking;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use base64::{engine::general_purpose, Engine as _};
use reqwest::{header::HeaderMap, Client};
use serde::Serialize;
use std::{env, str};
Expand All @@ -7,43 +6,45 @@ use tracing::debug;
const SEND_TRACKING_REQUEST_ERROR: &str = "Can't send tracking usage event";

#[derive(Debug, Serialize)]
struct MixpanelProperties {
token: String,
struct PosthogProperties {
language: String,
pkg_version: String,
os: String,
}

#[derive(Debug, Serialize)]
struct TrackingData {
api_key: String,
event: String,
properties: MixpanelProperties,
distinct_id: String,
properties: PosthogProperties,
}

pub(crate) fn track_usage() {
let properties = MixpanelProperties {
token: "24177ef1ec09ffea5cb6f68909c66a61".to_string(),
let properties = PosthogProperties {
language: "rs".to_string(),
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
os: env::consts::OS.to_string(),
};
let tracking_data = TrackingData {
event: "CNN".to_string(),
api_key: "phc_95PGQnbyatmj2TBRPWYfhbHfqB6wgZj5QRL8WY9gW20".to_string(),
distinct_id: "cargo-near".to_string(),
event: "contract".to_string(),
properties,
};
let serialized_data = serde_json::to_vec(&tracking_data).unwrap();
let base64_encoded_data = general_purpose::STANDARD.encode(&serialized_data);

let client = Client::new();

let mut headers = HeaderMap::new();
headers.insert("accept", "text/plain".parse().unwrap());
headers.insert("content-type", "application/json".parse().unwrap());

if tokio::runtime::Runtime::new()
.unwrap()
.block_on(
client
.get("https://api.mixpanel.com/track")
.query(&[("data", base64_encoded_data)])
.post("https://eu.i.posthog.com/capture")
.body(serialized_data)
.headers(headers)
.send(),
)
Expand Down

0 comments on commit 1a795ee

Please sign in to comment.