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

瀏覽器相容性

另見