Reporting API
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Note: This feature is available in Web Workers.
The Reporting API provides a generic reporting mechanism for web applications to use to make reports available based on various platform features (for example Content Security Policy, Permissions-Policy, or feature deprecation reports) in a consistent manner.
Concepts and usage
There are several different features and problems on the web platform that generate information useful to web developers when they are trying to fix bugs or improve their websites in other ways. Such information can include:
- Content Security Policy violations.
- Permissions-Policy violations.
- Integrity-Policy violations.
- Deprecated feature usage (when you are using something that will stop working soon in browsers).
- Occurrence of crashes.
- Occurrence of user-agent interventions (when the browser blocks something your code is trying to do because it is deemed a security risk for example, or just plain annoying, like auto-playing audio).
The purpose of the Reporting API is to provide a consistent reporting mechanism that can be used to make such information available to developers in the form of reports represented by JavaScript objects. There are a few ways to use it, which are detailed in the sections below.
Reporting server endpoints
Each unique origin you want to get reports for can be given a series of "endpoints", which are named URLs (or groups of URLs) that can be sent reports from a user agent. A reporting server at these endpoints can collect the reports, and process and present them as needed by your application.
The Reporting-Endpoints HTTP header is used to specify details about the different endpoints that a user-agent has available to it for delivering reports.
The report-to directive can then be used on particular HTTP response headers to indicate the specific endpoint that will be used for the associated report.
For example, the CSP report-to directive can be used on the Content-Security-Policy or Content-Security-Policy-Report-Only HTTP headers to specify the endpoint that CSP violation reports should be sent to.
Note: There is no absolute guarantee of report delivery — a report could still fail to be collected if a serious error occurs.
The reports themselves are sent to the target endpoint by the user agent in a POST operation with a Content-Type of application/reports+json.
They are serializations of Report objects, where the type indicates the type of report, the url indicates the origin of the report, and the body contains a serialization of the API interface that corresponds to the report type.
For example, CSP violation reports have a type of csp-violation and a body that is a serialization of a CSPViolationReportBody object.
Reports sent to endpoints can be retrieved independently of the running of the websites they relate to, which is useful — a crash for example could bring down a website and stop anything running, but a report could still be obtained to give the developer some clues as to why it happened.
Reporting observers
Reports can also be obtained via ReportingObserver objects created via JavaScript inside the website you are aiming to get reports on.
This method is not as failsafe as sending reports to the server because any page crash could stop you retrieving the reports — but it is easier to set up, and more flexible.
A ReportingObserver object is created using the ReportingObserver() constructor, which is passed two parameters:
- A callback function with two parameters — an array of the reports available in the observer's report queue and a copy of the same
ReportingObserverobject, which allows observation to be controlled directly from inside the callback. The callback runs when observation starts. - An options dictionary that allows you to specify the type of reports to collect, and whether reports generated before the observer was created should be observable (
buffered: true).
Methods are then available on the observer to start collecting reports (ReportingObserver.observe()), retrieve the reports currently in the report queue (ReportingObserver.takeRecords()), and disconnect the observer so it can no longer collect records (ReportingObserver.disconnect()).
Report types
Reports sent to reporting endpoints and reporting observers are essentially the same: they have an origin url, a type, and a body that is an instance of the interface corresponding with that type.
The only difference is that server reports are JSON serializations of the objects.
The mapping of report type to body is shown below.
type |
body |
Items reported |
|---|---|---|
deprecation |
DeprecationReportBody |
Deprecated features used by the site. |
integrity-violation |
IntegrityViolationReportBody |
Violations of the page's integrity policy. |
intervention |
InterventionReportBody |
Features blocked by the user agent, for example, if permissions are not granted. |
csp-violation |
CSPViolationReportBody |
Violations of the site's CSP policy. |
Generating reports via WebDriver
The Reporting API spec also defines a Generate Test Report WebDriver extension, which allows you to simulate report generation during automation. Reports generated via WebDriver are observed by any registered ReportObserver objects present in the loaded website. This is not yet documented.
Interfaces
DeprecationReportBody-
Contains details of deprecated web platform features that a website is using.