Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / assistant / useAssistantTelemetry.ts
blobf0c6fb0c3559cb24cb512e3e227dfc9aeeecd278
1 import useApi from '@proton/components/hooks/useApi';
2 import { TelemetryMailComposerAssistantEvents, TelemetryMeasurementGroups } from '@proton/shared/lib/api/telemetry';
3 import type {
4     ASSISTANT_TYPE,
5     ERROR_TYPE,
6     GENERATION_SELECTION_TYPE,
7     GENERATION_TYPE,
8     INCOMPATIBILITY_TYPE,
9 } from '@proton/shared/lib/assistant';
10 import { sendTelemetryReport } from '@proton/shared/lib/helpers/metrics';
12 import type { ASSISTANT_INSERT_TYPE } from 'proton-mail/hooks/assistant/useComposerAssistantGenerate';
14 const useAssistantTelemetry = () => {
15     const api = useApi();
17     const sendShowAssistantReport = () => {
18         void sendTelemetryReport({
19             api,
20             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
21             event: TelemetryMailComposerAssistantEvents.show_assistant,
22         });
23     };
25     const sendFreeTrialStart = () => {
26         void sendTelemetryReport({
27             api,
28             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
29             event: TelemetryMailComposerAssistantEvents.free_trial_start,
30         });
31     };
33     const sendDownloadAssistantReport = (downloadTime: number) => {
34         void sendTelemetryReport({
35             api,
36             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
37             event: TelemetryMailComposerAssistantEvents.download_model,
38             values: {
39                 download_time: downloadTime,
40             },
41         });
42     };
44     const sendUseAnswerAssistantReport = (insertionType: ASSISTANT_INSERT_TYPE) => {
45         void sendTelemetryReport({
46             api,
47             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
48             event: TelemetryMailComposerAssistantEvents.use_answer,
49             dimensions: {
50                 insertion_type: insertionType,
51             },
52         });
53     };
55     const sendNotUseAnswerAssistantReport = () => {
56         void sendTelemetryReport({
57             api,
58             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
59             event: TelemetryMailComposerAssistantEvents.not_use_answer,
60         });
61     };
63     const sendRequestAssistantReport = ({
64         assistantType,
65         generationType,
66         selectionType,
67         ingestionTime,
68         generationTime,
69         tokensGenerated,
70     }: {
71         assistantType: ASSISTANT_TYPE;
72         generationType: GENERATION_TYPE;
73         selectionType: GENERATION_SELECTION_TYPE;
74         ingestionTime: number;
75         generationTime: number;
76         tokensGenerated: number;
77     }) => {
78         void sendTelemetryReport({
79             api,
80             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
81             event: TelemetryMailComposerAssistantEvents.request_assistant,
82             values: {
83                 ingestion_time: ingestionTime,
84                 generation_time: generationTime,
85                 tokens_generated: tokensGenerated,
86             },
87             dimensions: {
88                 assistant_type: assistantType,
89                 generation_type: generationType,
90                 selection_type: selectionType,
91             },
92         });
93     };
95     const sendAssistantErrorReport = ({
96         assistantType,
97         errorType,
98     }: {
99         assistantType: ASSISTANT_TYPE;
100         errorType: ERROR_TYPE;
101     }) => {
102         void sendTelemetryReport({
103             api,
104             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
105             event: TelemetryMailComposerAssistantEvents.assistant_error,
106             dimensions: {
107                 assistant_type: assistantType,
108                 error_type: errorType,
109             },
110         });
111     };
113     const sendSendMessageAssistantReport = () => {
114         void sendTelemetryReport({
115             api,
116             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
117             event: TelemetryMailComposerAssistantEvents.send_message,
118         });
119     };
121     const sendPauseDownloadAssistantReport = () => {
122         void sendTelemetryReport({
123             api,
124             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
125             event: TelemetryMailComposerAssistantEvents.pause_download,
126         });
127     };
129     const sendLoadModelAssistantReport = (loadTime: number) => {
130         void sendTelemetryReport({
131             api,
132             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
133             event: TelemetryMailComposerAssistantEvents.load_model,
134             values: {
135                 load_time: loadTime,
136             },
137         });
138     };
140     const sendUnloadModelAssistantReport = () => {
141         void sendTelemetryReport({
142             api,
143             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
144             event: TelemetryMailComposerAssistantEvents.unload_model,
145         });
146     };
148     const sendIncompatibleAssistantReport = ({
149         incompatibilityType,
150     }: {
151         incompatibilityType: INCOMPATIBILITY_TYPE;
152     }) => {
153         void sendTelemetryReport({
154             api,
155             measurementGroup: TelemetryMeasurementGroups.mailComposerAssistant,
156             event: TelemetryMailComposerAssistantEvents.incompatible_assistant,
157             dimensions: {
158                 incompatibility_type: incompatibilityType,
159             },
160         });
161     };
163     return {
164         sendShowAssistantReport,
165         sendFreeTrialStart,
166         sendDownloadAssistantReport,
167         sendUseAnswerAssistantReport,
168         sendNotUseAnswerAssistantReport,
169         sendRequestAssistantReport,
170         sendAssistantErrorReport,
171         sendSendMessageAssistantReport,
172         sendPauseDownloadAssistantReport,
173         sendLoadModelAssistantReport,
174         sendUnloadModelAssistantReport,
175         sendIncompatibleAssistantReport,
176     };
179 export default useAssistantTelemetry;