Add command palette action events

Joseph T. Lyons created

Change summary

Cargo.lock                                    |  1 
crates/client/src/telemetry.rs                | 15 +++++++++++
crates/command_palette/Cargo.toml             |  3 +
crates/command_palette/src/command_palette.rs | 27 +++++++++++++++++---
4 files changed, 41 insertions(+), 5 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -1607,6 +1607,7 @@ name = "command_palette"
 version = "0.1.0"
 dependencies = [
  "anyhow",
+ "client",
  "collections",
  "ctor",
  "editor",

crates/client/src/telemetry.rs 🔗

@@ -129,6 +129,11 @@ pub enum Event {
         environment: &'static str,
         milliseconds_since_first_event: i64,
     },
+    Action {
+        source: &'static str,
+        action: String,
+        milliseconds_since_first_event: i64,
+    },
 }
 
 #[cfg(debug_assertions)]
@@ -420,6 +425,16 @@ impl Telemetry {
         }
     }
 
+    pub fn report_action_event(self: &Arc<Self>, source: &'static str, action: String) {
+        let event = Event::Action {
+            source,
+            action,
+            milliseconds_since_first_event: self.milliseconds_since_first_event(),
+        };
+
+        self.report_event(event)
+    }
+
     fn milliseconds_since_first_event(&self) -> i64 {
         let mut state = self.state.lock();
 

crates/command_palette/Cargo.toml 🔗

@@ -9,6 +9,7 @@ path = "src/command_palette.rs"
 doctest = false
 
 [dependencies]
+client = { path = "../client" }
 collections = { path = "../collections" }
 editor = { path = "../editor" }
 fuzzy = {  path = "../fuzzy" }
@@ -16,9 +17,9 @@ gpui = { path = "../gpui" }
 picker = { path = "../picker" }
 project = { path = "../project" }
 settings = { path = "../settings" }
+theme = { path = "../theme" }
 ui = { path = "../ui" }
 util = { path = "../util" }
-theme = { path = "../theme" }
 workspace = { path = "../workspace" }
 zed_actions = { path = "../zed_actions" }
 anyhow.workspace = true

crates/command_palette/src/command_palette.rs 🔗

@@ -3,6 +3,7 @@ use std::{
     sync::Arc,
 };
 
+use client::telemetry::Telemetry;
 use collections::{CommandPaletteFilter, HashMap};
 use fuzzy::{StringMatch, StringMatchCandidate};
 use gpui::{
@@ -39,11 +40,18 @@ impl CommandPalette {
             let Some(previous_focus_handle) = cx.focused() else {
                 return;
             };
-            workspace.toggle_modal(cx, move |cx| CommandPalette::new(previous_focus_handle, cx));
+            let telemetry = workspace.client().telemetry().clone();
+            workspace.toggle_modal(cx, move |cx| {
+                CommandPalette::new(previous_focus_handle, telemetry, cx)
+            });
         });
     }
 
-    fn new(previous_focus_handle: FocusHandle, cx: &mut ViewContext<Self>) -> Self {
+    fn new(
+        previous_focus_handle: FocusHandle,
+        telemetry: Arc<Telemetry>,
+        cx: &mut ViewContext<Self>,
+    ) -> Self {
         let filter = cx.try_global::<CommandPaletteFilter>();
 
         let commands = cx
@@ -66,8 +74,12 @@ impl CommandPalette {
             })
             .collect();
 
-        let delegate =
-            CommandPaletteDelegate::new(cx.view().downgrade(), commands, previous_focus_handle);
+        let delegate = CommandPaletteDelegate::new(
+            cx.view().downgrade(),
+            commands,
+            telemetry,
+            previous_focus_handle,
+        );
 
         let picker = cx.new_view(|cx| Picker::new(delegate, cx));
         Self { picker }
@@ -103,6 +115,7 @@ pub struct CommandPaletteDelegate {
     commands: Vec<Command>,
     matches: Vec<StringMatch>,
     selected_ix: usize,
+    telemetry: Arc<Telemetry>,
     previous_focus_handle: FocusHandle,
 }
 
@@ -130,6 +143,7 @@ impl CommandPaletteDelegate {
     fn new(
         command_palette: WeakView<CommandPalette>,
         commands: Vec<Command>,
+        telemetry: Arc<Telemetry>,
         previous_focus_handle: FocusHandle,
     ) -> Self {
         Self {
@@ -138,6 +152,7 @@ impl CommandPaletteDelegate {
             matches: vec![],
             commands,
             selected_ix: 0,
+            telemetry,
             previous_focus_handle,
         }
     }
@@ -284,6 +299,10 @@ impl PickerDelegate for CommandPaletteDelegate {
         }
         let action_ix = self.matches[self.selected_ix].candidate_id;
         let command = self.commands.swap_remove(action_ix);
+
+        self.telemetry
+            .report_action_event("command palette", command.name.clone());
+
         self.matches.clear();
         self.commands.clear();
         cx.update_global(|hit_counts: &mut HitCounts, _| {