注册归因触发器

了解如何注册归因触发器以统计转化次数。

归因触发器是告知浏览器捕获转化的事件。

按照本文中的步骤,您可以注册触发器来记录转化,而浏览器随后会将这些转化归因于相关来源事件,即广告展示或广告点击。

注册方法

要注册触发器,请使用 HTML 元素或 JavaScript 调用:

  • <a> 标记
  • <img> 标记
  • <script> 标记
  • fetch 个电话素材资源
  • XMLHttpRequest
  • window.open

这会生成网络请求,您随后会使用触发器注册 HTTP 响应标头对其进行响应。

注册触发器以归因转化

注册触发器与注册归因来源事件类似。下文介绍了完整步骤。摘要如下:

  1. 启动触发器注册。使用像素或 fetch() 调用发出请求。
  2. 使用触发器注册标头进行响应,完成触发器注册

    收到像素请求(发送到在常规 src 属性中定义的端点或 attributionsrc 中定义的端点(如果您已选择使用 attributionsrc 并为其指定值)后,系统会使用标头 Attribution-Reporting-Register-Trigger 进行响应。

    在此标头中,指定您希望最终在报告中显示的触发器数据。任何响应都可以设置此标头。只要它是对从符合 destination 的网站发出的请求的响应,就将匹配来源。收到标头后,浏览器会查找匹配的来源并安排报告。

    事件级报告示例

    {
      "event_trigger_data": [{
        "trigger_data": "[unsigned 64-bit integer]",
        "priority": "[signed 64-bit integer]",
        "deduplication_key": "[unsigned 64-bit integer]"
      }]
    }
    ```
    
    **Example for summary reports:**
    
    ```json
    {
      ... // existing fields, such as "event_trigger_data"
    
      "aggregatable_trigger_data": [
       {
         "key_piece": "0x400",
         "source_keys": ["campaignCounts"]
       },
       {
         "key_piece": "0xA80",
         "source_keys": ["geoValue", "nonMatchingKeyIdsAreIgnored"]
       }
      ],
      "aggregatable_values": {
        "campaignCounts": 32768,
        "geoValue": 1664
      }
    }
    ```
    

处理子网域

如果 destinationhttps://advertiser.example,则 https://advertiser.example 及其子网域(例如 https://shop.advertiser.example)上的转化都可以归因。

如果 destinationhttps://shop.advertiser.example,则可以对 https://advertiser.examplehttps://shop.advertiser.example 上的转化进行归因。

必需属性和可选属性

当您使用 HTML 元素或进行 JavaScript 调用以注册触发器时,您可能需要使用 attributionsrcattributionReporting。如需详细了解何时必须使用这些功能,请参阅下表。

attributionsrc 为可选值时,使用它表示请求符合归因报告的条件。如果您使用 attributionsrc,浏览器会发送 Attribution-Reporting-Eligible 标头。这对于应用到网站的衡量也很有用:如果存在 attributionsrc,浏览器会发送 Attribution-Reporting-Support 标头。

注册方法 触发器
<a> 标记 不适用 锚点无法注册触发器。
<img> 标记 attributionsrc可选项。该标头足以注册触发器。
<script> 标记 attributionsrc可选项。该标头足以注册触发器。
fetch 个电话素材资源 attributionReporting 选项为必需选项。
XMLHttpRequest attributionReporting 选项为必需选项。
window.open() 不适用 window.open 无法注册触发器。

第 1 步:启动触发器注册

您可以使用像素(<img> 代码)或脚本代码注册触发器。

使用新的或现有的转化像素

<img src="https://ad-tech.example/conversionpixel"
     attributionsrc="https://adtech.example/attribution_trigger?purchase=13">

使用脚本标记

您可以使用脚本标记执行触发器注册;其行为与 <img> 相同。以下代码示例说明了 fetch()XMLHttpRequest() (XHR) 的用法。

此代码有效地模拟了使用 attributionsrc 的 HTML 请求的作用:

// With fetch
const attributionReporting = {
  eventSourceEligible: false,
  triggerEligible: true,
};

// Optionally set keepalive to ensure the request outlives the page.
window.fetch("https://adtech.example/attribution_source?my_ad_id=123",
  { keepalive: true, attributionReporting });
// With XMLHttpRequest:
const attributionReporting = {
  eventSourceEligible: false,
  triggerEligible: true,
};

const req = new XMLHttpRequest();
  req.open('GET', url);
  req.setAttributionReporting(
      attributionReporting);
  req.send();

包含或不包含值的 attributionsrc

您可以添加 attributionsrc(无论是否包含值)。

<!-- Without a value -->
<img src="..." width="1" height="1" attributionsrc>

<!--With a value (URL) -->
<img src="..." width="1" height="1" attributionsrc="https://...">

如果您为 attributionsrc 设置值,此值应为单个网址。

使用网址会导致浏览器发起单独的 keepalive 提取请求(每个网址对应一个),其中包含 Attribution-Reporting-Eligible 请求标头。

如果您想通过响应独立于元素主请求的请求进行来源注册,这会非常有用。

例如,如果您需要注册锚点元素点击的来源,实际上可能无法控制目的地;在这种情况下,您需要一个配置,通过该配置,将来源注册标头作为与导航分开的请求的响应发送,并且您可以完全控制。通过为 attributionsrc 指定显式值,即可指示浏览器发出该额外请求并配置其目的地。

第 2 步:使用标头回复

收到浏览器请求后,进行响应并在响应中加入 Attribution-Reporting-Register-Trigger 标头:

JSON.stringify({event_trigger_data: [{
      trigger_data: '412444888111012',
      // Optional
      priority: '1000000000000',
      deduplication_key: '2345698765'
    }], debug_key: '1115698977'})

后续步骤

了解如何注册归因来源