Learn how to implement Topics API to observe and access user's topic of interest with code samples.
Implementation status
- The Topics API has completed the public discussion phase and is currently available to 99 percent of users, scaling up to 100 percent.
- To provide your feedback on the Topics API, create an Issue on the Topics explainer or participate in discussions in the Improving Web Advertising Business Group. The explainer has a number of open questions that still require further definition.
- The Privacy Sandbox timeline provides implementation timelines for the Topics API and other Privacy Sandbox proposals.
- Topics API: latest updates details changes and enhancements to the Topics API and implementations.
Try the demo
There are two Topics API demos that enable you to try out Topics as a single user.
- JavaScript API demo: topics-demo.glitch.me.
- Header demo: topics-fetch-demo.glitch.me
You can also run the Topics colab to try out the Topics classifier model.
Use the JavaScript API to access topics and record them as observed
The Topics JavaScript API has one method: document.browsingTopics()
. This has two purposes:
- Tell the browser to record the current page visit as having been observed for the caller, so this can later be used to calculate topics for the user (for the caller).
- Access topics for the user that have been observed by the caller.
The method returns a promise that resolves to an array of up to three topics, one for each of the three most recent epochs, in random order. An epoch is a period of time, set to one week in Chrome's implementation.
Each topic object in the array returned by document.browsingTopics()
has these properties:
configVersion
: a string identifying the current Topics API configuration, for examplechrome.2
modelVersion
: a string identifying the machine-learning classifier used to infer topics for the site, for example4
taxonomyVersion
: a string identifying the set of topics used by the browser, for example2
topic
: a number identifying the topic in the taxonomy, for example309
version
: a string concatenatingconfigVersion
,taxonomyVersion
, andmodelVersion
, for examplechrome.2:2:4
The parameters described in this guide, and details of the API (such as taxonomy size, the number of topics calculated per week and the number of topics returned per call) are subject to change as we incorporate ecosystem feedback and iterate on the API.
Detect support for document.browsingTopics
Before using the API, check if it's supported by the browser and available in the document:
'browsingTopics' in document && document.featurePolicy.allowsFeature('browsing-topics') ?
console.log('document.browsingTopics() is supported on this page') :
console.log('document.browsingTopics() is not supported on this page');
Access topics with the JavaScript API
Here is a basic example of possible API usage to access topics for the current user.
try {
// Get the array of top topics for this user.
const topics = await document.browsingTopics();
// Request an ad creative, providing topics information.
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.
} catch (error) {
// Handle error.
}
Access topics without modifying state
document.browsingTopics()
returns topics observed by the caller for the current user. By default, the method also causes the browser to record the current page visit as observed by the caller, so it can later be used in topics calculation. From Chrome 108, the method can be passed an optional argument to skip the page visit from being recorded: {skipObservation:true}
.
In other words, {skipObservation:true}
means the method call won't cause the current page to be included in the calculation of topics.
Use headers to access topics and mark them as observed
You can access topics, and also mark page visits as observed, with the help of request and response headers.
Using the header approach can be much more performant than using the JavaScript API, since the API requires creating a cross-origin iframe and making a document.browsingTopics()
call from it. (A cross-origin iframe must be used for the call, because the context from which the API is invoked is used to ensure the browser returns the topics appropriate to the caller.) The Topics explainer has further discussion: Should there be a way to send topics using Fetch as a request header? .
Topics can be accessed from the Sec-Browsing-Topics
header of a fetch()
or XHR
request.
Setting an Observe-Browsing-Topics: ?1
header on the response to the request
causes the browser to record the current page visit as observed by the caller,
so it can later be used in topics calculation.
Topics can be accessed and observed with HTTP headers in two ways:
fetch()
: Add{browsingTopics: true}
to the options parameter of afetch()
request. The Topics header demo shows a simplified example of this.- iframe attribute: Add the
browsingtopics
attribute to an<iframe>
element, or set the equivalent JavaScript propertyiframe.browsingTopics = true
. The registrable domain of the iframe source is the caller domain: for example, for<iframe src="https://example.com" browsingtopics></iframe>
: the caller isexample.com
.
Some additional notes about headers:
- Redirects will be followed, and the topics sent in the redirect request will be specific to the redirect URL.
- The request header won't modify state for the caller unless there is a corresponding response header. That is, without the response header, the page visit won't be recorded as observed, so it won't affect the user's topic calculation for the next epoch.
- The response header is only honored if the corresponding request included the topics header.
- The URL of the request provides the registrable domain that is recorded as the caller domain.
Next steps
- Learn more about what topics are and how they work.
- Try out the demo.
See also
Check out our resources to better understand the Topics API on the Web.
- Check out Topics demos, collab and walkthrough videos.
- See the list of Chrome flags that allow developers to customize the Topics API for testing.
- See how users and developers can control the API.
- Check out the resources for technical explainers and support. Ask questions, engage and share feedback.