Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: Implement telemetry on Search commands (#43563)

Jeremias Menichellib2fdc7687fe7
diff --git a/apps/docs/hooks/useDocsCommandMenuTelemetry.ts b/apps/docs/hooks/useDocsCommandMenuTelemetry.ts
index 2a471cbc1a3..45c2283ec04 100644
--- a/apps/docs/hooks/useDocsCommandMenuTelemetry.ts
+++ b/apps/docs/hooks/useDocsCommandMenuTelemetry.ts
@@ -3,19 +3,20 @@
import { useCallback } from 'react'
import type {
- CommandMenuOpenedEvent,
+ CommandMenuClosedEvent,
CommandMenuCommandClickedEvent,
+ CommandMenuOpenedEvent,
CommandMenuSearchSubmittedEvent,
} from 'common/telemetry-constants'
import { useSendTelemetryEvent } from '@/lib/telemetry'
export function useDocsCommandMenuTelemetry() {
const sendTelemetryEvent = useSendTelemetryEvent()
-
const onTelemetry = useCallback(
(
event:
| CommandMenuOpenedEvent
+ | CommandMenuClosedEvent
| CommandMenuCommandClickedEvent
| CommandMenuSearchSubmittedEvent
) => {
diff --git a/apps/studio/hooks/misc/useStudioCommandMenuTelemetry.ts b/apps/studio/hooks/misc/useStudioCommandMenuTelemetry.ts
index 4b5457a9eea..922679e85b4 100644
--- a/apps/studio/hooks/misc/useStudioCommandMenuTelemetry.ts
+++ b/apps/studio/hooks/misc/useStudioCommandMenuTelemetry.ts
@@ -4,8 +4,9 @@ import { useParams } from 'common'
import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
import type {
- CommandMenuOpenedEvent,
+ CommandMenuClosedEvent,
CommandMenuCommandClickedEvent,
+ CommandMenuOpenedEvent,
CommandMenuSearchSubmittedEvent,
} from 'common/telemetry-constants'
@@ -18,6 +19,7 @@ export function useStudioCommandMenuTelemetry() {
(
event:
| CommandMenuOpenedEvent
+ | CommandMenuClosedEvent
| CommandMenuCommandClickedEvent
| CommandMenuSearchSubmittedEvent
) => {
diff --git a/apps/www/hooks/useWwwCommandMenuTelemetry.ts b/apps/www/hooks/useWwwCommandMenuTelemetry.ts
index a2bb7a625be..257fea454e8 100644
--- a/apps/www/hooks/useWwwCommandMenuTelemetry.ts
+++ b/apps/www/hooks/useWwwCommandMenuTelemetry.ts
@@ -1,8 +1,9 @@
import { useCallback } from 'react'
import type {
- CommandMenuOpenedEvent,
+ CommandMenuClosedEvent,
CommandMenuCommandClickedEvent,
+ CommandMenuOpenedEvent,
CommandMenuSearchSubmittedEvent,
} from 'common/telemetry-constants'
import { useSendTelemetryEvent } from 'lib/telemetry'
@@ -14,6 +15,7 @@ export function useWwwCommandMenuTelemetry() {
(
event:
| CommandMenuOpenedEvent
+ | CommandMenuClosedEvent
| CommandMenuCommandClickedEvent
| CommandMenuSearchSubmittedEvent
) => {
diff --git a/packages/common/telemetry-constants.ts b/packages/common/telemetry-constants.ts
index 7ef30fddebd..2c543cee321 100644
--- a/packages/common/telemetry-constants.ts
+++ b/packages/common/telemetry-constants.ts
@@ -2339,6 +2339,14 @@ export interface CommandMenuCommandClickedEvent {
command_name: string
command_value?: string
command_type: 'action' | 'route'
+ /**
+ * The search query that was active when the command was clicked
+ */
+ search_query?: string
+ /**
+ * The path or URL the clicked item leads to (only present for route commands)
+ */
+ result_path?: string
/**
* In which app the command input was typed
*/
@@ -2347,6 +2355,24 @@ export interface CommandMenuCommandClickedEvent {
groups: Partial<TelemetryGroups>
}
+/**
+ * User closed the command menu.
+ *
+ * @group Events
+ * @source studio, docs, www
+ * @page any
+ */
+export interface CommandMenuClosedEvent {
+ action: 'command_menu_closed'
+ properties: {
+ /**
+ * In which app the command menu was closed
+ */
+ app: 'studio' | 'docs' | 'www'
+ }
+ groups: Partial<TelemetryGroups>
+}
+
/**
* User opened a sidebar panel.
*
@@ -2980,6 +3006,7 @@ export type TelemetryEvent =
| TableCreateGeneratePoliciesExperimentConvertedEvent
| AuthUsersSearchSubmittedEvent
| CommandMenuOpenedEvent
+ | CommandMenuClosedEvent
| CommandMenuSearchSubmittedEvent
| CommandMenuCommandClickedEvent
| InlineEditorSettingClickedEvent
diff --git a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
index 91e406768d4..b66e1696151 100644
--- a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
+++ b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
@@ -235,10 +235,23 @@ function CommandMenu({ children, trigger }: CommandMenuProps) {
const query = useQuery()
const setQuery = useSetQuery()
+ const telemetryContext = useCommandMenuTelemetryContext()
+
const { ref: contentRef } = useTouchGestures({ toggleOpen: () => setOpen(!open) })
+ const handleOpenChange = (newOpen: boolean) => {
+ if (!newOpen && open && telemetryContext?.onTelemetry) {
+ telemetryContext.onTelemetry({
+ action: 'command_menu_closed',
+ properties: { app: telemetryContext.app },
+ groups: {},
+ })
+ }
+ setOpen(newOpen)
+ }
+
return (
- <Dialog open={open} onOpenChange={setOpen}>
+ <Dialog open={open} onOpenChange={handleOpenChange}>
{trigger}
<DialogContent
id="command-menu-dialog-content"
@@ -246,10 +259,10 @@ function CommandMenu({ children, trigger }: CommandMenuProps) {
forceMount
ref={contentRef}
onOpenAutoFocus={(e) => isMobile && e.preventDefault()}
- onInteractOutside={() => setOpen(false)}
+ onInteractOutside={() => handleOpenChange(false)}
onEscapeKeyDown={(e) => {
e.preventDefault()
- return query ? setQuery('') : page ? popPage() : setOpen(false)
+ return query ? setQuery('') : page ? popPage() : handleOpenChange(false)
}}
size={size}
className={cn(
diff --git a/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetry.ts b/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetry.ts
index 06d6cb996f3..90bc52ece17 100644
--- a/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetry.ts
+++ b/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetry.ts
@@ -1,6 +1,7 @@
'use client'
import type {
+ CommandMenuClosedEvent,
CommandMenuCommandClickedEvent,
CommandMenuOpenedEvent,
CommandMenuSearchSubmittedEvent,
@@ -11,7 +12,11 @@ import { useCommandMenuTelemetryContext } from './useCommandMenuTelemetryContext
import { useCommandMenuOpen } from './viewHooks'
export type CommandMenuTelemetryCallback = (
- event: CommandMenuOpenedEvent | CommandMenuCommandClickedEvent | CommandMenuSearchSubmittedEvent
+ event:
+ | CommandMenuOpenedEvent
+ | CommandMenuClosedEvent
+ | CommandMenuCommandClickedEvent
+ | CommandMenuSearchSubmittedEvent
) => void
export interface UseCommandMenuTelemetryOptions {
@@ -64,7 +69,7 @@ export const useCommandMenuOpenedTelemetry: (
action: 'command_menu_opened' as const,
properties: {
trigger_type: trigger,
- location: 'user_dropdown_menu',
+ trigger_location: 'user_dropdown_menu',
app: telemetryContext.app,
},
groups: {},
diff --git a/packages/ui-patterns/src/CommandMenu/index.tsx b/packages/ui-patterns/src/CommandMenu/index.tsx
index e41f375511d..98e4833b493 100644
--- a/packages/ui-patterns/src/CommandMenu/index.tsx
+++ b/packages/ui-patterns/src/CommandMenu/index.tsx
@@ -20,6 +20,7 @@ export {
useCommandMenuOpenedTelemetry,
useCommandMenuTelemetry,
} from './api/hooks/useCommandMenuTelemetry'
+export { useCommandMenuTelemetryContext } from './api/hooks/useCommandMenuTelemetryContext'
export { useHistoryKeys } from './api/hooks/useHistoryKeys'
export * from './api/hooks/viewHooks'
export * from './api/utils'
diff --git a/packages/ui-patterns/src/CommandMenu/internal/Command.tsx b/packages/ui-patterns/src/CommandMenu/internal/Command.tsx
index d6000e9f2d3..f7ce30bcb04 100644
--- a/packages/ui-patterns/src/CommandMenu/internal/Command.tsx
+++ b/packages/ui-patterns/src/CommandMenu/internal/Command.tsx
@@ -5,6 +5,7 @@ import { cn, CommandItem_Shadcn_ } from 'ui'
import { useCommandMenuTelemetryContext } from '../api/hooks/useCommandMenuTelemetryContext'
import { useCrossCompatRouter } from '../api/hooks/useCrossCompatRouter'
+import { useQuery } from '../api/hooks/queryHooks'
import { useResetCommandMenu, useSetCommandMenuOpen } from '../api/hooks/viewHooks'
import type { IActionCommand, ICommand, IRouteCommand } from './types'
@@ -56,6 +57,7 @@ const CommandItem = forwardRef<
const setIsOpen = useSetCommandMenuOpen()
const resetCommandMenu = useResetCommandMenu()
const telemetryContext = useCommandMenuTelemetryContext()
+ const query = useQuery()
const command = _command as ICommand // strip the readonly applied from the proxy
@@ -68,6 +70,8 @@ const CommandItem = forwardRef<
command_name: command.name,
command_value: command.value,
command_type: isActionCommand(command) ? ('action' as const) : ('route' as const),
+ search_query: query || undefined,
+ result_path: isRouteCommand(command) ? command.route : undefined,
app: telemetryContext.app,
},
groups: {},
diff --git a/packages/ui-patterns/src/CommandMenu/prepackaged/DocsSearch/DocsSearchPage.tsx b/packages/ui-patterns/src/CommandMenu/prepackaged/DocsSearch/DocsSearchPage.tsx
index afba52261c1..cf75d9bb293 100644
--- a/packages/ui-patterns/src/CommandMenu/prepackaged/DocsSearch/DocsSearchPage.tsx
+++ b/packages/ui-patterns/src/CommandMenu/prepackaged/DocsSearch/DocsSearchPage.tsx
@@ -7,7 +7,7 @@ import {
useDocsSearch,
} from 'common'
import { Book, ChevronRight, Github, Hash, Loader2, MessageSquare, Search } from 'lucide-react'
-import { useEffect, useRef } from 'react'
+import { useCallback, useEffect, useRef } from 'react'
import { Button, CommandGroup_Shadcn_, CommandItem_Shadcn_, CommandList_Shadcn_, cn } from 'ui'
import { StatusIcon } from 'ui/src/components/StatusIcon'
@@ -20,6 +20,7 @@ import {
generateCommandClassNames,
TextHighlighter,
useCrossCompatRouter,
+ useCommandMenuTelemetryContext,
useQuery,
useSetCommandMenuOpen,
useSetQuery,
@@ -75,10 +76,29 @@ const DocsSearchPage = () => {
const setQuery = useSetQuery()
const query = useQuery()
+ const telemetryContext = useCommandMenuTelemetryContext()
+
const initialLoad = useRef(true)
const inputRef = useRef<HTMLInputElement>(null)
const router = useCrossCompatRouter()
+ const trackResultClicked = useCallback(
+ function trackResultClicked(name: string, path: string) {
+ telemetryContext?.onTelemetry?.({
+ action: 'command_menu_command_clicked',
+ properties: {
+ command_name: name,
+ command_type: 'route',
+ search_query: query || undefined,
+ result_path: path,
+ app: telemetryContext.app,
+ },
+ groups: {},
+ })
+ },
+ [query]
+ )
+
async function openLink(pageType: PageType, link: string) {
switch (pageType) {
case PageType.Markdown:
@@ -182,6 +202,7 @@ const DocsSearchPage = () => {
key={`${page.path}-item`}
value={`${escapeAttributeSelector(page.title)}-item-index-${i}`}
onSelect={() => {
+ trackResultClicked(page.title, page.path)
openLink(page.type, page.path)
}}
forceMount={true}
@@ -211,6 +232,10 @@ const DocsSearchPage = () => {
'ml-3 mb-3'
)}
onSelect={() => {
+ trackResultClicked(
+ section.heading ?? page.title,
+ formatSectionUrl(page, section)
+ )
openLink(page.type, formatSectionUrl(page, section))
}}
key={`${page.path}__${section.heading}-item`}