1
+ /**
2
+ * Utility module for reporting metrics and exceptions to the native layer.
3
+ *
4
+ * This module provides standardized functions for sending metric events and exception reports
5
+ * through the messaging system. It includes predefined metric names for common error types
6
+ * and helper functions to construct and send metric events.
7
+ *
8
+ * Please see https://duckduckgo.github.io/content-scope-scripts/interfaces/shared_messages.reportmetricnotification
9
+ * for the message schema
10
+ *
11
+ * @example
12
+ * ```javascript
13
+ * import { reportMetric, reportException } from './report-metric.js';
14
+ *
15
+ * // Report a custom metric
16
+ * reportMetric(messaging, {
17
+ * metricName: 'userAction',
18
+ * params: { action: 'buttonClick', page: 'home' }
19
+ * });
20
+ *
21
+ * // Report an exception
22
+ * reportException(messaging, {
23
+ * message: 'Failed to load user data',
24
+ * kind: 'NetworkError'
25
+ * });
26
+ * ```
27
+ *
28
+ * @module Report Metric
29
+ */
30
+
31
+ /** The message ID used for reporting metrics to the native layer */
1
32
export const REPORT_METRIC_MESSAGE_ID = 'reportMetric' ;
33
+
34
+ /** The metric name used for exception reporting */
2
35
export const METRIC_NAME_EXCEPTION = 'exception' ;
3
- /* Error Types */
36
+
37
+ /** Metric name for generic errors */
4
38
export const METRIC_NAME_GENERIC_ERROR = 'Error' ;
39
+
40
+ /** Metric name for initialization errors */
5
41
export const METRIC_NAME_INIT_ERROR = 'InitError' ;
42
+
43
+ /** Metric name for initial setup errors */
6
44
export const METRIC_NAME_INITIAL_SETUP_ERROR = 'InitialSetupError' ;
45
+
46
+ /** Metric name for messaging-related errors */
7
47
export const METRIC_NAME_MESSAGING_ERROR = 'MessagingError' ;
48
+
49
+ /** Metric name for video overlay errors */
8
50
export const METRIC_NAME_VIDEO_OVERLAY_ERROR = 'VideoOverlayError' ;
9
51
10
52
/**
@@ -15,9 +57,20 @@ export const METRIC_NAME_VIDEO_OVERLAY_ERROR = 'VideoOverlayError';
15
57
*/
16
58
17
59
/**
18
- * Sends a standard 'reportMetric' event to the native layer
19
- * @param {SharedMessaging } messaging
20
- * @param {ReportMetricEvent } metricEvent
60
+ * Sends a standard 'reportMetric' event to the native layer.
61
+ *
62
+ * @param {SharedMessaging } messaging - The messaging instance used to communicate with the native layer
63
+ * @param {ReportMetricEvent } metricEvent - The metric event to report, must contain a metricName
64
+ * @throws {Error } When messaging.notify is not defined
65
+ * @throws {Error } When metricEvent.metricName is missing
66
+ *
67
+ * @example
68
+ * ```javascript
69
+ * reportMetric(messaging, {
70
+ * metricName: 'pageLoad',
71
+ * params: { duration: 1500, page: 'home' }
72
+ * });
73
+ * ```
21
74
*/
22
75
export function reportMetric ( messaging , metricEvent ) {
23
76
if ( ! messaging ?. notify ) {
@@ -32,9 +85,27 @@ export function reportMetric(messaging, metricEvent) {
32
85
}
33
86
34
87
/**
35
- * Sends a 'reportMetric' event with metric name 'exception'
36
- * @param {SharedMessaging } messaging
37
- * @param {ExceptionMetric['params'] } params
88
+ * Sends a 'reportMetric' event with metric name 'exception'.
89
+ *
90
+ * This is a convenience function for reporting exceptions. It constructs
91
+ * an exception metric with the provided parameters and sends it using the standard
92
+ * reportMetric function. If message or kind parameters are not provided, default values
93
+ * will be used.
94
+ *
95
+ * @param {SharedMessaging } messaging - The messaging instance used to communicate with the native layer
96
+ * @param {{message?: string, kind?: string} } params - The exception parameters containing message and kind (both optional)
97
+ *
98
+ * @example
99
+ * ```javascript
100
+ * // Report an exception with custom message and kind
101
+ * reportException(messaging, {
102
+ * message: 'Failed to fetch user data from API',
103
+ * kind: 'NetworkError'
104
+ * });
105
+ *
106
+ * // Report an exception with default values (message: 'unknown error', kind: 'Error')
107
+ * reportException(messaging, {});
108
+ * ```
38
109
*/
39
110
export function reportException ( messaging , params ) {
40
111
console . log ( 'reportException' , params ) ;
0 commit comments