XMLHttpRequest:setAttributionReporting() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

XMLHttpRequest 接口的 setAttributionReporting() 方法表示您希望请求的响应能够注册一个基于 JavaScript 的归因源归因触发器

有关更多详细信息,请参阅 归因报告 API

语法

js
setAttributionReporting(options)

参数

options

提供归因报告选项的对象,包括以下属性:

eventSourceEligible

一个布尔值。如果设置为 true,则请求的响应有资格注册归因源。如果设置为 false,则没有。

triggerEligible

一个布尔值。如果设置为 true,则请求的响应有资格注册归因触发器。如果设置为 false,则没有。

返回值

无 (undefined)。

异常

InvalidStateError DOMException

如果在关联的 XMLHttpRequest 尚未打开,或者已经发送,则抛出。

TypeError DOMException

如果 归因报告 API 的使用被 attribution-reporting Permissions-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

浏览器兼容性

另见