Document: browsingTopics() method - Web APIs | MDN
Syntax
js
browsingTopics()
browsingTopics(options)
Parameters
optionsOptional-
An options object, which can contain the following properties:
skipObservation-
A boolean value that, if set to
true, causes the browser to not observe topics whenbrowsingTopics()is invoked. The default isfalse, which causes topics to be observed.
Return value
A Promise that fulfills with an array of up to three objects representing the current user's selected topics for the last three epochs. Each object contains the following properties:
configVersion-
A string identifying the algorithm (other than the model part) used to calculate the topic.
modelVersion-
A string representing the model used to classify a string (such as a web page's hostname) into topic IDs.
taxonomyVersion-
A string representing the taxonomy version used.
topic-
A number representing the ID of the topic, which can be used by the browser to retrieve the topic from the taxonomy (see an example taxonomy of interests).
version-
The
configVersion,modelVersion, andtaxonomyVersion, concatenated with colons (:) between each.
The exact property values may vary by browser implementation. An example object from Chrome might look as follows:
json
{
"configVersion": "chrome.1",
"modelVersion": "1",
"taxonomyVersion": "1",
"topic": 43,
"version": "chrome.1:1:1"
}
Exceptions
NotAllowedErrorDOMException-
Thrown if:
- Usage of the Topics API is disallowed by a
browsing-topicsPermissions Policy. - The calling site does not have the Topics API included in a successful privacy sandbox enrollment process.
- Usage of the Topics API is disallowed by a
Examples
js
// Get an array of top topics for this user
const topics = await document.browsingTopics();
// Request an ad creative
const response = await fetch("https://ads.example/get-creative", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(topics),
});
// Get the JSON from the response
const creative = await response.json();
// Display ad
Specifications
This feature is not part of an official standard, although it is specified in the Topics API Unofficial Proposal Draft.