Skip to content

Commit

Permalink
chore(analytics): instrument command bar (#19323)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr authored Dec 14, 2023
1 parent a86a31a commit f0aa25c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
9 changes: 9 additions & 0 deletions frontend/src/lib/components/CommandBar/actionBarLogic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterMount, beforeUnmount, connect, kea, listeners, path } from 'kea'
import { subscriptions } from 'kea-subscriptions'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'

import { commandPaletteLogic } from '../CommandPalette/commandPaletteLogic'
import type { actionBarLogicType } from './actionBarLogicType'
Expand All @@ -24,6 +25,8 @@ export const actionBarLogic = kea<actionBarLogicType>([
'onMouseEnterResult',
'onMouseLeaveResult',
],
eventUsageLogic,
['reportCommandBarActionSearch', 'reportCommandBarActionResultExecuted'],
],
values: [
commandBarLogic,
Expand All @@ -44,6 +47,12 @@ export const actionBarLogic = kea<actionBarLogicType>([
// listen on hide action from legacy palette, and hide command bar
actions.hideCommandBar()
},
setInput: ({ input }) => {
actions.reportCommandBarActionSearch(input)
},
executeResult: ({ result }) => {
actions.reportCommandBarActionResultExecuted(result.display)
},
})),
subscriptions(({ values, actions }) => ({
barStatus: (value, oldvalue) => {
Expand Down
14 changes: 13 additions & 1 deletion frontend/src/lib/components/CommandBar/commandBarLogic.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { actions, afterMount, beforeUnmount, kea, path, reducers } from 'kea'
import { actions, afterMount, beforeUnmount, connect, kea, path, reducers } from 'kea'
import { subscriptions } from 'kea-subscriptions'
import { shouldIgnoreInput } from 'lib/utils'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'

import type { commandBarLogicType } from './commandBarLogicType'
import { BarStatus } from './types'

export const commandBarLogic = kea<commandBarLogicType>([
path(['lib', 'components', 'CommandBar', 'commandBarLogic']),
connect({
actions: [eventUsageLogic, ['reportCommandBarStatusChanged']],
}),
actions({
setCommandBar: (status: BarStatus, initialQuery?: string) => ({ status, initialQuery }),
hideCommandBar: true,
Expand Down Expand Up @@ -36,6 +41,13 @@ export const commandBarLogic = kea<commandBarLogicType>([
},
],
}),
subscriptions(({ actions }) => ({
barStatus: (status, prevStatus) => {
if (prevStatus !== undefined) {
actions.reportCommandBarStatusChanged(status)
}
},
})),
afterMount(({ actions, cache }) => {
// register keyboard shortcuts
cache.onKeyDown = (event: KeyboardEvent) => {
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/lib/components/CommandBar/searchBarLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { loaders } from 'kea-loaders'
import { router } from 'kea-router'
import { subscriptions } from 'kea-subscriptions'
import api, { CountedPaginatedResponse } from 'lib/api'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { urls } from 'scenes/urls'

import { groupsModel } from '~/models/groupsModel'
Expand Down Expand Up @@ -46,7 +47,12 @@ export const searchBarLogic = kea<searchBarLogicType>([
path(['lib', 'components', 'CommandBar', 'searchBarLogic']),
connect({
values: [commandBarLogic, ['initialQuery', 'barStatus'], groupsModel, ['groupTypes', 'aggregationLabel']],
actions: [commandBarLogic, ['hideCommandBar', 'setCommandBar', 'clearInitialQuery']],
actions: [
commandBarLogic,
['hideCommandBar', 'setCommandBar', 'clearInitialQuery'],
eventUsageLogic,
['reportCommandBarSearch', 'reportCommandBarSearchResultOpened'],
],
}),
actions({
search: true,
Expand All @@ -59,13 +65,15 @@ export const searchBarLogic = kea<searchBarLogicType>([
setIsAutoScrolling: (scrolling: boolean) => ({ scrolling }),
openResult: (index: number) => ({ index }),
}),
loaders(({ values }) => ({
loaders(({ values, actions }) => ({
rawSearchResponse: [
null as SearchResponse | null,
{
loadSearchResponse: async (_, breakpoint) => {
await breakpoint(DEBOUNCE_MS)

actions.reportCommandBarSearch(values.searchQuery.length)

if (clickhouseTabs.includes(values.activeTab)) {
// prevent race conditions when switching tabs quickly
return values.rawSearchResponse
Expand Down Expand Up @@ -424,6 +432,7 @@ export const searchBarLogic = kea<searchBarLogicType>([
const result = values.combinedSearchResults![index]
router.actions.push(urlForResult(result))
actions.hideCommandBar()
actions.reportCommandBarSearchResultOpened(result.type)
},
})),
subscriptions(({ values, actions }) => ({
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/components/CommandBar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum BarStatus {
SHOW_SHORTCUTS = 'show_shortcuts',
}

export type ResultType = SearchableEntity | 'person'
export type ResultType = SearchableEntity | 'person' | 'group'

export type PersonResult = {
type: 'person'
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/lib/utils/eventUsageLogic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { actions, connect, kea, listeners, path } from 'kea'
import { BarStatus, ResultType } from 'lib/components/CommandBar/types'
import { convertPropertyGroupToProperties, isGroupPropertyFilter } from 'lib/components/PropertyFilters/utils'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import type { Dayjs } from 'lib/dayjs'
Expand Down Expand Up @@ -491,6 +492,12 @@ export const eventUsageLogic = kea<eventUsageLogicType>([
reportOnboardingProductSelected: (productKey: string) => ({ productKey }),
reportOnboardingCompleted: (productKey: string) => ({ productKey }),
reportSubscribedDuringOnboarding: (productKey: string) => ({ productKey }),
// command bar
reportCommandBarStatusChanged: (status: BarStatus) => ({ status }),
reportCommandBarSearch: (queryLength: number) => ({ queryLength }),
reportCommandBarSearchResultOpened: (type: ResultType) => ({ type }),
reportCommandBarActionSearch: (query: string) => ({ query }),
reportCommandBarActionResultExecuted: (resultDisplay) => ({ resultDisplay }),
}),
listeners(({ values }) => ({
reportAxisUnitsChanged: (properties) => {
Expand Down Expand Up @@ -1200,5 +1207,21 @@ export const eventUsageLogic = kea<eventUsageLogicType>([
product_key: productKey,
})
},
// command bar
reportCommandBarStatusChanged: ({ status }) => {
posthog.capture('command bar status changed', { status })
},
reportCommandBarSearch: ({ queryLength }) => {
posthog.capture('command bar search', { queryLength })
},
reportCommandBarSearchResultOpened: ({ type }) => {
posthog.capture('command bar search result opened', { type })
},
reportCommandBarActionSearch: ({ query }) => {
posthog.capture('command bar action search', { query })
},
reportCommandBarActionResultExecuted: ({ resultDisplay }) => {
posthog.capture('command bar search result executed', { resultDisplay })
},
})),
])

0 comments on commit f0aa25c

Please sign in to comment.