Skip to content

Commit 4590bf5

Browse files
committed
Docs
1 parent 54b04f1 commit 4590bf5

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

messaging/lib/test-utils.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export function mockAndroidMessaging(params) {
292292
* @param {string} secret
293293
* @return {Promise<void>}
294294
*/
295-
// eslint-disable-next-line require-await
295+
296296
process: async (jsonString, secret) => {
297297
/** @type {RequestMessage | NotificationMessage} */
298298
const msg = JSON.parse(jsonString);

special-pages/shared/report-metric.js

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
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 */
132
export const REPORT_METRIC_MESSAGE_ID = 'reportMetric';
33+
34+
/** The metric name used for exception reporting */
235
export const METRIC_NAME_EXCEPTION = 'exception';
3-
/* Error Types */
36+
37+
/** Metric name for generic errors */
438
export const METRIC_NAME_GENERIC_ERROR = 'Error';
39+
40+
/** Metric name for initialization errors */
541
export const METRIC_NAME_INIT_ERROR = 'InitError';
42+
43+
/** Metric name for initial setup errors */
644
export const METRIC_NAME_INITIAL_SETUP_ERROR = 'InitialSetupError';
45+
46+
/** Metric name for messaging-related errors */
747
export const METRIC_NAME_MESSAGING_ERROR = 'MessagingError';
48+
49+
/** Metric name for video overlay errors */
850
export const METRIC_NAME_VIDEO_OVERLAY_ERROR = 'VideoOverlayError';
951

1052
/**
@@ -15,9 +57,20 @@ export const METRIC_NAME_VIDEO_OVERLAY_ERROR = 'VideoOverlayError';
1557
*/
1658

1759
/**
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+
* ```
2174
*/
2275
export function reportMetric(messaging, metricEvent) {
2376
if (!messaging?.notify) {
@@ -32,9 +85,27 @@ export function reportMetric(messaging, metricEvent) {
3285
}
3386

3487
/**
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+
* ```
38109
*/
39110
export function reportException(messaging, params) {
40111
console.log('reportException', params);

typedoc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const config = {
3434
'special-pages/pages/new-tab/app/favorites/constants.js',
3535
'special-pages/pages/**/types/*.ts',
3636
'special-pages/shared/types/*.ts',
37+
'special-pages/shared/report-metric.js',
3738
],
3839
categoryOrder: ['Special Pages', 'Content Scope Scripts Integrations', 'Other'],
3940
out: 'docs',

0 commit comments

Comments
 (0)