What Is the Attribution Reporting API?
Advertisers often want to track how well their ads perform: How many users saw the ad? How many converted into purchases? Traditional tracking methods rely on third-party cookies, which compromise user privacy by sharing data across domains. The Attribution Reporting API solves this by enabling conversion tracking without exposing users’ personal browsing data.
Key Features
- Tracks conversions (e.g., clicks, purchases) without third-party cookies
- Stores data in a private local cache within the browser
- Limits data sharing to specific parties (e.g., the advertiser or ad tech provider)
- Requires secure contexts (HTTPS) to function.
How It Works
- Attribution Sources: When a user interacts with an ad (e.g., clicks a link), the browser stores source data locally.
- Attribution Triggers: When a conversion occurs (e.g., adding an item to a cart), the browser matches it with the attribution source.
- Reports: The browser sends anonymized reports to a designated endpoint for analysis.
Components of the API
HTTP Headers
The API uses HTTP headers to register attribution sources and triggers.
- Attribution-Reporting-Eligible: Marks requests as eligible for attribution reporting.
- Attribution-Reporting-Register-Source: Registers an attribution source (e.g., ad click).
- Attribution-Reporting-Register-Trigger: Registers an attribution trigger (e.g., conversion).
HTML Attributes
The `attributionsrc` attribute on elements like `<a>`, `<img>`, and `<script>` specifies attribution data.
JavaScript Methods
You can programmatically register sources and triggers using `fetch()` or `XMLHttpRequest` with the appropriate options.
JavaScript
// Trigger attribution when a conversion occurs (e.g., "Add to Cart" click)
const registerTrigger = () => {
fetch('https://shop.example/register-trigger', {
method: 'POST',
headers: {
'Attribution-Reporting-Eligible': 'trigger',
'Content-Type': 'application/json'
},
body: JSON.stringify({
event: 'add_to_cart',
productId: '12345',
value: 49.99
})
}).then((response) => {
if (response.ok) {
console.log('Attribution trigger registered successfully');
} else {
console.error('Failed to register attribution trigger');
}
});
};
// Simulate conversion trigger
document
.querySelector('#add-to-cart')
.addEventListener('click', registerTrigger);
// Server-Side: Responding with Attribution Headers
// On the server, include the appropriate headers in the response to mark attribution sources or triggers.
// HTTP/1.1 200 OK
// Attribution-Reporting-Register-Source: source-event-id=1234, destination=https://shop.example
// For triggers:
// HTTP/1.1 200 OK
// Attribution-Reporting-Register-Trigger: trigger-data=conversion-id-5678