Skip to content

Commit e0fc9c2

Browse files
shakyShanemgurgel
authored andcommitted
top-level metrics class
1 parent 1dd5230 commit e0fc9c2

File tree

23 files changed

+161
-97
lines changed

23 files changed

+161
-97
lines changed

injected/src/features/duck-player-native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DuckPlayerNativeMessages } from './duckplayer-native/messages.js';
44
import { setupDuckPlayerForNoCookie, setupDuckPlayerForSerp, setupDuckPlayerForYouTube } from './duckplayer-native/sub-feature.js';
55
import { Environment } from './duckplayer/environment.js';
66
import { Logger } from './duckplayer/util.js';
7-
import { MetricsReporter, EXCEPTION_KIND_INITIAL_SETUP_ERROR } from '../../../special-pages/shared/metrics/metrics-reporter.js';
7+
import { MetricsReporter, EXCEPTION_KIND_INITIAL_SETUP_ERROR } from '../../../metrics/metrics-reporter.js';
88

99
/**
1010
* @import {DuckPlayerNativeSubFeature} from './duckplayer-native/sub-feature.js'

injected/src/features/duck-player.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { DuckPlayerOverlayMessages, OpenInDuckPlayerMsg, Pixel } from './duckpla
3737
import { isBeingFramed } from '../utils.js';
3838
import { initOverlays } from './duckplayer/overlays.js';
3939
import { Environment } from './duckplayer/environment.js';
40-
import { MetricsReporter } from '../../../special-pages/shared/metrics/metrics-reporter.js';
40+
import { MetricsReporter } from '../../../metrics/metrics-reporter.js';
4141

4242
/**
4343
* @typedef UserValues - A way to communicate user settings

injected/src/features/duckplayer-native/messages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as constants from './constants.js';
2-
import { MetricsReporter, EXCEPTION_KIND_MESSAGING_ERROR } from 'special-pages/shared/metrics/metrics-reporter.js';
2+
import { MetricsReporter, EXCEPTION_KIND_MESSAGING_ERROR } from '../../../..//metrics/metrics-reporter.js';
33

44
/** @import {YouTubeError} from './error-detection.js' */
55
/** @import {Environment} from '../duckplayer/environment.js' */

injected/src/features/duckplayer/overlay-messages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable promise/prefer-await-to-then */
22
import * as constants from './constants.js';
3-
import { MetricsReporter, EXCEPTION_KIND_MESSAGING_ERROR } from 'special-pages/shared/metrics/metrics-reporter.js';
3+
import { MetricsReporter, EXCEPTION_KIND_MESSAGING_ERROR } from '../../../../metrics/metrics-reporter.js';
44

55
/**
66
* @typedef {import("@duckduckgo/messaging").Messaging} Messaging

injected/src/features/duckplayer/overlays.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DomState } from './util.js';
22
import { ClickInterception, Thumbnails } from './thumbnails.js';
33
import { VideoOverlay } from './video-overlay.js';
44
import { registerCustomElements } from './components/index.js';
5-
import { EXCEPTION_KIND_INITIAL_SETUP_ERROR } from 'special-pages/shared/metrics/metrics-reporter.js';
5+
import { EXCEPTION_KIND_INITIAL_SETUP_ERROR } from '../../../../metrics/metrics-reporter.js';
66

77
/**
88
* @typedef {object} OverlayOptions

injected/unit-test/verify-artifacts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const ROOT = join(cwd(import.meta.url), '..', '..');
77
console.log(ROOT);
88
const BUILD = join(ROOT, 'build');
99

10-
let CSS_OUTPUT_SIZE = 770_000;
10+
let CSS_OUTPUT_SIZE = 780_000;
1111
if (process.platform === 'win32') {
1212
CSS_OUTPUT_SIZE = CSS_OUTPUT_SIZE * 1.1; // 10% larger for Windows due to line endings
1313
}

metrics/docs/readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Metrics
3+
---
4+
5+
# Metrics
6+
7+
Utility class for reporting metrics and exceptions to the native layer.
8+
9+
This class provides standardized methods for sending metric events and exception reports
10+
through the messaging system. It includes predefined metric names for common error types
11+
and helper methods to construct and send metric events.
12+
13+
Please see [metrics-reporter.js](../metrics-reporter.js)
14+
for the message schema
15+
16+
{@includeCode ../examples/metrics.js}

metrics/examples/metrics.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { MetricsReporter } from '../metrics-reporter.js';
2+
import { Messaging, MessagingContext, TestTransportConfig } from '@duckduckgo/messaging';
3+
4+
const messaging = createMessaging();
5+
const metrics = new MetricsReporter(messaging);
6+
7+
// Report a custom metric
8+
metrics.reportMetric({
9+
metricName: 'exception',
10+
params: { kind: 'abc', message: 'something went' },
11+
});
12+
13+
// Report an exception
14+
metrics.reportException({
15+
message: 'Failed to load user data',
16+
kind: 'NetworkError',
17+
});
18+
19+
// Report an exception by passing an Error object
20+
metrics.reportExceptionWithError(new Error('Missing params'));
21+
22+
// test messaging example
23+
function createMessaging() {
24+
const context = new MessagingContext({
25+
context: 'test',
26+
env: 'development',
27+
featureName: 'testFeature',
28+
});
29+
const config = new TestTransportConfig({
30+
notify() {},
31+
request() {
32+
return Promise.resolve(null);
33+
},
34+
subscribe() {
35+
return () => {};
36+
},
37+
});
38+
return new Messaging(context, config);
39+
}

special-pages/shared/metrics/metrics-reporter.js renamed to metrics/metrics-reporter.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,7 @@
1-
/**
2-
* Utility class for reporting metrics and exceptions to the native layer.
3-
*
4-
* This class provides standardized methods for sending metric events and exception reports
5-
* through the messaging system. It includes predefined metric names for common error types
6-
* and helper methods 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 { MetricsReporter } from './metrics-reporter.js';
14-
*
15-
* const metrics = new MetricsReporter(messaging);
16-
*
17-
* // Report a custom metric
18-
* metrics.reportMetric({
19-
* metricName: 'userAction',
20-
* params: { action: 'buttonClick', page: 'home' }
21-
* });
22-
*
23-
* // Report an exception
24-
* metrics.reportException({
25-
* message: 'Failed to load user data',
26-
* kind: 'NetworkError'
27-
* });
28-
*
29-
* // Report an exception by passing an Error object
30-
* metrics.reportExceptionWithError(new Error('Missing params'));
31-
* ```
32-
*
33-
* @module Metrics Reporter
34-
*/
35-
361
/**
372
* @typedef {import('./types/metrics.ts').ExceptionMetric} ExceptionMetric
383
* @typedef {import('./types/metrics.ts').ReportMetricEvent} ReportMetricEvent
394
*/
40-
415
import { createTypedMessages } from '@duckduckgo/messaging/lib/typed-messages.js';
426

437
/** Exception kind for generic errors */

0 commit comments

Comments
 (0)