Skip to content

Commit 6f7b291

Browse files
feat: track AI related events COMPASS-7109, COMPASS-7110 (#4739)
* feat: track the number of edits a user makes to a generated query COMPASS-7109 * feat: track prompt submitted COMPASS-7110 * refactor: address pr comments * refactor: track all find options
1 parent 81f0c52 commit 6f7b291

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

packages/compass-query-bar/src/components/option-editor.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type OptionEditorProps = {
8585
hasAutofix?: boolean;
8686
onChange: (value: string) => void;
8787
onApply?(): void;
88+
onBlur?(): void;
8889
placeholder?: string | HTMLElement;
8990
schemaFields?: CompletionWithServerInfo[];
9091
serverVersion?: string;
@@ -99,6 +100,7 @@ const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
99100
hasAutofix,
100101
onChange,
101102
onApply,
103+
onBlur,
102104
placeholder,
103105
schemaFields = [],
104106
serverVersion = '3.6.0',
@@ -188,6 +190,7 @@ const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
188190
data-testid={dataTestId}
189191
onFocus={onFocus}
190192
onPaste={onPaste}
193+
onBlur={onBlur}
191194
/>
192195
{showInsights && insights && (
193196
<div className={queryBarEditorOptionInsightsStyles}>

packages/compass-query-bar/src/components/query-ai.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('QueryAI Component', function () {
130130
expect(screen.queryByTestId(feedbackPopoverTextAreaId)).to.not.exist;
131131
expect(trackingLogs).to.deep.equal([
132132
{
133-
event: 'AIQuery Feedback',
133+
event: 'AI Query Feedback',
134134
properties: {
135135
feedback: 'positive',
136136
text: 'this is the query I was looking for',

packages/compass-query-bar/src/components/query-ai.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const onSubmitFeedback = (feedback: 'positive' | 'negative', text: string) => {
1818
text,
1919
});
2020

21-
track('AIQuery Feedback', () => ({
21+
track('AI Query Feedback', () => ({
2222
feedback,
2323
text,
2424
}));

packages/compass-query-bar/src/components/query-option.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback } from 'react';
1+
import React, { useCallback, useRef } from 'react';
22
import type { Signal } from '@mongodb-js/compass-components';
33
import {
44
Label,
@@ -16,6 +16,8 @@ import type { QueryOption as QueryOptionType } from '../constants/query-option-d
1616
import { changeField } from '../stores/query-bar-reducer';
1717
import type { QueryProperty } from '../constants/query-properties';
1818
import type { RootState } from '../stores/query-bar-store';
19+
import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging';
20+
const { track } = createLoggerAndTelemetry('COMPASS-QUERY-BAR-UI');
1921

2022
const queryOptionStyles = css({
2123
display: 'flex',
@@ -117,6 +119,9 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
117119
insights,
118120
}) => {
119121
const darkMode = useDarkMode();
122+
const editorInitialValueRef = useRef<string | undefined>(value);
123+
const editorCurrentValueRef = useRef<string | undefined>(value);
124+
editorCurrentValueRef.current = value;
120125

121126
const optionDefinition = OPTION_DEFINITION[name];
122127
const isDocumentEditor = optionDefinition.type === 'document';
@@ -131,6 +136,17 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
131136
[name, onChange]
132137
);
133138

139+
const onBlurEditor = useCallback(() => {
140+
if (
141+
!!editorCurrentValueRef.current &&
142+
editorCurrentValueRef.current !== editorInitialValueRef.current &&
143+
(editorInitialValueRef.current || editorCurrentValueRef.current !== '{}')
144+
) {
145+
track('Query Edited', { option_name: name });
146+
editorInitialValueRef.current = editorCurrentValueRef.current;
147+
}
148+
}, [name, editorInitialValueRef]);
149+
134150
return (
135151
<div
136152
className={cx(
@@ -167,6 +183,7 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
167183
hasError={hasError}
168184
id={id}
169185
onChange={onValueChange}
186+
onBlur={onBlurEditor}
170187
placeholder={placeholder}
171188
value={value}
172189
data-testid={`query-bar-option-${name}-input`}
@@ -194,6 +211,7 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
194211
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
195212
onValueChange(evt.currentTarget.value)
196213
}
214+
onBlur={onBlurEditor}
197215
placeholder={placeholder as string}
198216
{...props}
199217
/>

packages/compass-query-bar/src/modules/change-filter.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,51 @@ describe('changeFilter', function () {
102102
});
103103
});
104104
});
105+
106+
describe('clearValue', function () {
107+
it('should clear a value in the filter state', function () {
108+
const filter = { name: 'pineapple' };
109+
expect(
110+
changeFilter('clearValue', filter, {
111+
field: 'name',
112+
})
113+
).to.not.have.property('name');
114+
});
115+
});
116+
117+
describe('setValue', function () {
118+
it('should set a value in the filter state when the field does not exist', function () {
119+
expect(
120+
changeFilter(
121+
'setValue',
122+
{},
123+
{
124+
field: 'name',
125+
value: 'strawberry',
126+
}
127+
)
128+
).to.have.property('name', 'strawberry');
129+
});
130+
131+
it('should unset a new value in the filter state when unsetIfSet arg is sent', function () {
132+
const filter = { name: 'strawberry' };
133+
expect(
134+
changeFilter('setValue', filter, {
135+
field: 'name',
136+
value: 'strawberry',
137+
unsetIfSet: true,
138+
})
139+
).to.not.have.property('name');
140+
});
141+
142+
it('should set a new value in the filter state when the field already exists', function () {
143+
const filter = { name: 'pineapple' };
144+
expect(
145+
changeFilter('setValue', filter, {
146+
field: 'name',
147+
value: 'maracuja',
148+
})
149+
).to.have.property('name', 'maracuja');
150+
});
151+
});
105152
});

packages/compass-query-bar/src/stores/ai-query-reducer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { QueryFormFields } from '../constants/query-properties';
1212
import { DEFAULT_FIELD_VALUES } from '../constants/query-bar-store';
1313
import { openToast } from '@mongodb-js/compass-components';
1414

15-
const { log, mongoLogId } = createLoggerAndTelemetry('AI-QUERY-UI');
15+
const { log, mongoLogId, track } = createLoggerAndTelemetry('AI-QUERY-UI');
1616

1717
type AIQueryStatus = 'ready' | 'fetching' | 'success';
1818

@@ -112,6 +112,11 @@ export const runAIQuery = (
112112
Promise<void>,
113113
AIQueryStartedAction | AIQueryFailedAction | AIQuerySucceededAction
114114
> => {
115+
track('AI Prompt Submitted', () => ({
116+
editor_view_type: 'find',
117+
user_input_length: userInput.length,
118+
}));
119+
115120
return async (dispatch, getState, { dataService, atlasService }) => {
116121
const {
117122
aiQuery: { aiQueryFetchId: existingFetchId },

0 commit comments

Comments
 (0)