Note: This feature is available in Web Workers.
The ReportingObserver() constructor of the Reporting API creates a new ReportingObserver object instance, which can be used to collect and access reports.
new ReportingObserver(callback)
new ReportingObserver(callback, options)callbackA callback function that runs when the observer starts to collect reports (i.e., via ReportingObserver.observe()). The callback function is given two parameters:
reportsA sequence of objects representing the reports collected in the observer's report queue.
Report objects are expected to have the following properties:
bodyAn object representing the body of the report. The structure of the report (in particular of its body), depends on its type.
typeA string indicating the type of the report. For information on report types see options.types below.
urlA string representing the URL of the document that generated the report.
observerA reference to the same ReportingObserver object, allowing for recursive report collection, and so on.
options OptionalAn object allowing you to set the options for creating the object. The available options are:
typesAn array of strings representing the types of report to be collected by this observer. Available types include:
coepViolations of the site's Cross-Origin-Embedder-Policy (COEP). Reports are COEPViolationReport instances.
coopViolations of the site's Cross-Origin-Opener-Policy (COOP). Reports are COOPViolationReport instances.
crashBrowser crash reports. Reports are CrashReport instances. Note that crash reports aren't retrievable via a ReportingObserver but can be sent to a server.
csp-violationViolations of the site's CSP policy. Reports are CSPViolationReport instances.
deprecationDeprecated features used by the site. Reports are DeprecationReport instances.
integrity-violationViolations of the page's integrity policy. Reports are IntegrityViolationReport instances.
interventionFeatures blocked by the user agent, for example, if an ad significantly impacts page performance. Reports are InterventionReport instances.
permissions-policy-violationViolations of the site's Permissions-Policy. Reports are PermissionsPolicyViolationReport instances.
If this option is omitted, all supported types are collected.
bufferedA boolean that defines whether the reports that were generated before the observer was able to be created should be observable (true) or not (false).
This code shows how to create a ReportingObserver that could be used to observe deprecation and integrity-violation reports.
const options = {
types: ["deprecation", "integrity-violation"],
buffered: true,
};
const observer = new ReportingObserver((reports, observer) => {
reportBtn.onclick = () => displayReports(reports);
}, options);