XMLHttpRequest:setAttributionReporting() 方法
XMLHttpRequest 接口的 setAttributionReporting() 方法表示您希望请求的响应能够注册一个基于 JavaScript 的归因源或归因触发器。
有关更多详细信息,请参阅 归因报告 API。
语法
js
setAttributionReporting(options)
参数
options-
提供归因报告选项的对象,包括以下属性:
eventSourceEligible-
一个布尔值。如果设置为
true,则请求的响应有资格注册归因源。如果设置为false,则没有。 triggerEligible-
一个布尔值。如果设置为
true,则请求的响应有资格注册归因触发器。如果设置为false,则没有。
返回值
无 (undefined)。
异常
InvalidStateErrorDOMException-
如果在关联的
XMLHttpRequest尚未打开,或者已经发送,则抛出。 TypeErrorDOMException-
如果 归因报告 API 的使用被
attribution-reportingPermissions-Policy阻止,则抛出。
示例
js
const attributionReporting = {
eventSourceEligible: true,
triggerEligible: false,
};
function triggerSourceInteraction() {
const req = new XMLHttpRequest();
req.open("GET", "https://shop.example/endpoint");
// Check availability of setAttributionReporting() before calling
if (typeof req.setAttributionReporting === "function") {
req.setAttributionReporting(attributionReporting);
req.send();
} else {
throw new Error("Attribution reporting not available");
// Include recovery code here as appropriate
}
}
// Associate the interaction trigger with whatever
// element and event makes sense for your code
elem.addEventListener("click", triggerSourceInteraction);
规范
| 规范 |
|---|
| 归因报告 # dom-xmlhttprequest-setattributionreporting |
浏览器兼容性
加载中…