Set staff user property in telemetry

Co-authored-by: Joseph Lyons <joseph@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-10-03 10:52:57 -07:00
parent 761ae3ae6f
commit 4477f95ee6
7 changed files with 49 additions and 15 deletions

View File

@ -351,7 +351,7 @@ impl Client {
}));
}
Status::SignedOut | Status::UpgradeRequired => {
self.telemetry.set_metrics_id(None);
self.telemetry.set_authenticated_user_info(None, false);
state._reconnect_task.take();
}
_ => {}

View File

@ -9,6 +9,7 @@ use isahc::Request;
use lazy_static::lazy_static;
use parking_lot::Mutex;
use serde::Serialize;
use serde_json::json;
use std::{
io::Write,
mem,
@ -176,11 +177,32 @@ impl Telemetry {
.detach();
}
pub fn set_metrics_id(&self, metrics_id: Option<String>) {
pub fn set_authenticated_user_info(
self: &Arc<Self>,
metrics_id: Option<String>,
is_staff: bool,
) {
let is_signed_in = metrics_id.is_some();
self.state.lock().metrics_id = metrics_id.map(|s| s.into());
if is_signed_in {
self.report_event_with_user_properties(
"$identify",
Default::default(),
json!({ "$set": { "staff": is_staff } }),
)
}
}
pub fn report_event(self: &Arc<Self>, kind: &str, properties: Value) {
self.report_event_with_user_properties(kind, properties, Default::default());
}
fn report_event_with_user_properties(
self: &Arc<Self>,
kind: &str,
properties: Value,
user_properties: Value,
) {
if AMPLITUDE_API_KEY.is_none() {
return;
}
@ -198,7 +220,11 @@ impl Telemetry {
} else {
None
},
user_properties: None,
user_properties: if let Value::Object(user_properties) = user_properties {
Some(user_properties)
} else {
None
},
user_id: state.metrics_id.clone(),
device_id: state.device_id.clone(),
os_name: state.os_name,

View File

@ -157,6 +157,7 @@ impl FakeServer {
.receipt(),
GetPrivateUserInfoResponse {
metrics_id: "the-metrics-id".into(),
staff: false,
},
)
.await;

View File

@ -148,7 +148,14 @@ impl UserStore {
let fetch_metrics_id =
client.request(proto::GetPrivateUserInfo {}).log_err();
let (user, info) = futures::join!(fetch_user, fetch_metrics_id);
client.telemetry.set_metrics_id(info.map(|i| i.metrics_id));
if let Some(info) = info {
client.telemetry.set_authenticated_user_info(
Some(info.metrics_id),
info.staff,
);
} else {
client.telemetry.set_authenticated_user_info(None, false);
}
client.telemetry.report_event("sign in", Default::default());
current_user_tx.send(user).await.ok();
}

View File

@ -1738,7 +1738,16 @@ impl Server {
.await
.user_id_for_connection(request.sender_id)?;
let metrics_id = self.app_state.db.get_user_metrics_id(user_id).await?;
response.send(proto::GetPrivateUserInfoResponse { metrics_id })?;
let user = self
.app_state
.db
.get_user_by_id(user_id)
.await?
.ok_or_else(|| anyhow!("user not found"))?;
response.send(proto::GetPrivateUserInfoResponse {
metrics_id,
staff: user.admin,
})?;
Ok(())
}

View File

@ -1221,16 +1221,6 @@ mod tests {
let project_store = cx.add_model(|_| ProjectStore::new(project::Db::open_fake()));
let server = FakeServer::for_client(current_user_id, &client, cx).await;
let request = server.receive::<proto::GetPrivateUserInfo>().await.unwrap();
server
.respond(
request.receipt(),
proto::GetPrivateUserInfoResponse {
metrics_id: "the-metrics-id".into(),
},
)
.await;
let fs = FakeFs::new(cx.background());
fs.insert_tree("/private_dir", json!({ "one.rs": "" }))
.await;

View File

@ -755,6 +755,7 @@ message GetPrivateUserInfo {}
message GetPrivateUserInfoResponse {
string metrics_id = 1;
bool staff = 2;
}
// Entities