@duckduckgo/content-scope-scripts
    Preparing search index...

    Class MetricsReporter

    Class for reporting metrics and exceptions to the native layer.

    Index

    Constructors

    Properties

    messaging: SharedMessaging
    DEFAULT_EXCEPTION_MESSAGE: "Unknown error" = ...

    The default exception message

    MESSAGE_ID: "reportMetric" = ...

    The message ID used for reporting metrics to the native layer

    METRIC_NAME_EXCEPTION: "exception" = ...

    The metric name used for exception reporting

    Methods

    • Sends a reportMetric event with metric name exception, getting message and kind from the params object. If no params object is passed, defaults are used.

      Parameters

      • Optionalparams: ExceptionMetricParams

        The exception parameters containing message and kind (both optional)

      Returns void

      // Report an exception with custom message and kind
      metrics.reportException({
      message: 'Failed to fetch user data from API',
      kind: 'NetworkError'
      });

      // Report an exception with default values (message: 'Unknown error', kind: 'Error')
      metrics.reportException();
    • Sends a reportMetric event with metric name exception, getting message and kind from the error object. The kind property is inferred from error.name.

      If no error object is passed, a default error is reported.

      If an invalid error object is passed, nothing is reported.

      Parameters

      • Optionalerror: Error

        The error to report

      Returns void

      const error = new Error('Failed to fetch user data from API');
      error.name = 'NetworkError';
      metrics.reportExceptionWithError(error);
    • Sends a standard reportMetric event to the native layer.

      Parameters

      • metricEvent: ExceptionMetric

        The metric event to report, must contain a metricName

      Returns void

      When metricEvent.metricName is missing

      metrics.reportMetric({
      metricName: 'pageLoad',
      params: { duration: 1500, page: 'home' }
      });