注册归因来源

了解如何注册来源,以便将点击和观看归因于相应事件。

归因来源是指与广告相关的事件(点击或观看),广告技术平台可以在其中附加以下类型的信息:

  • 情境报告数据,例如广告素材 ID、广告系列相关信息或地理位置。
  • 转化目标网址,即您希望用户转化的网站。

按照本文档中的步骤操作,您可以注册来源(广告展示或点击),浏览器随后会将转化归因于这些来源。

注册方法

如需注册归因来源,请使用 HTML 元素或 JavaScript 调用:

  • <a> 标记
  • <img> 标记
  • <script> 标记
  • fetch 次来电
  • XMLHttpRequest
  • window.open 次来电

这会生成网络请求,然后您可以使用来源注册 HTTP 响应标头来响应这些请求。

为点击或观看注册来源

如需注册点击或观看的归因来源,请按照此处所述的步骤操作。以下是完整步骤。总结如下:

  1. 启动来源注册。使用 HTML 元素或 JavaScript 调用发出请求。点击和观看的此步骤有所不同,您会在以下部分中注意到这一点。
  2. 通过使用来源注册标头进行响应,完成来源注册。收到该请求后,使用标头 Attribution-Reporting-Register-Source 进行响应。在该标头中,指定所选的 Attribution Reporting 配置。无论是点击还是观看,此步骤都相同。

    摘要报告示例

    {
      "aggregation_keys": {
        "campaignCounts": "0x159",
        "geoValue": "0x5"
      },
      "aggregatable_report_window": "86400",
      "destination": "https://example.com"
    }
    

    事件级报告示例

    {
      "source_event_id": "12340873456",
      "destination": "[eTLD+1]",
      "expiry": "[64-bit signed integer]",
      "priority": "[64-bit signed integer]",
      "event_report_window": "[64-bit signed integer]"
    }
    

必需属性和可选属性

当您使用 HTML 元素或进行 JavaScript 调用来注册来源时,可能需要使用 attributionsrcattributionReporting。如需详细了解何时需要提供这些信息,请参阅下表。

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

注册方法 来源
<a> 标记 (导航来源)
attributionsrc必需的。
<img> 标记 (事件源)
attributionsrc必需的。
<script> 标记 (事件源)
attributionsrc必需的。
fetch 次来电 attributionReporting 选项是必需的。
XMLHttpRequest attributionReporting 选项是必需的。
window.open 次来电 (导航来源)
attributionsrc必需的

第 1 步:启动来源注册

第 1 步对于点击和观看有所不同。

如需为点击注册归因来源,您可以使用 <a> 标记或 JavaScript window.open()

使用锚点

attributionsrc 添加到您要衡量展示次数或点击次数的现有 <a> 代码中:

<a href="https://shoes.example/..." attributionsrc>Click me</a>

如需了解详情,请查看示例代码

使用脚本

使用 attributionsrc 调用 window.open()

window.open(
  "https://shoes.example/...",
  "_blank",
  "attributionsrc");

为了让系统将此方法纳入考虑范围,必须在用户互动后的 5 秒内调用此方法。

对于图片或脚本,您可以指定单个网址值,而不是单独添加 attributionsrc

<a href=... attributionsrc="https://a.example/register-source">Click me</a>

在 JavaScript 示例中,如果您为 attributionsrc 提供值,请务必对该网址进行编码,以防其中包含特殊字符(例如 =)而导致参数解析不正确。

编码方式如下:

const encodedUrl = encodeURIComponent(
  "https://adtech.example/attribution_source?ad_id=...");
window.open(
  "https://shoes.example/landing",
   "_blank",
   `attributionsrc=${encodedUrl}`);

attributionsrc 还可以接受以空格分隔的网址列表,如下面的 <a> 标记所示:

<a href=... attributionsrc="https://a.example/register-source
  https://b.example/register-source">Click me</a>

或者,如本例所示,使用 window.open()

window.open("...", "_blank", `attributionsrc=${encodedUrl1}
  attributionsrc=${encodedUrl2}`)

在这种情况下,这两个网址都会收到符合导航源条件的 attributionsrc 请求(包含 Attribution-Reporting-Eligible 标头的请求)。

attributionsrc(无论是否具有值)

如您之前所见,您可以指定不含网址的 attributionsrc。您还可以指定单个网址。此外,您还可以使用以空格分隔的网址列表。

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

如果您想通过响应与元素的主要请求分开的请求来注册来源,此方法非常有用。

例如,如果您需要为锚元素上的点击注册来源,但实际上无法控制目的地,那么您需要一种配置,让您可以完全控制发送来源注册标头作为对与导航分开的请求的响应。通过为 attributionsrc 指定明确的值,您可以指示浏览器发出该额外请求并配置其目标位置。

如需为视图注册归因来源,您可以使用图片或脚本标记,并向其中添加 attributionsrc 属性。

或者,您也可以使用 JavaScript fetch()XMLHttpRequest()

含图片

<img attributionsrc
src="https://adtech.example/attribution_source?ad_id=...">

使用脚本

<script attributionsrc
  src="https://adtech.example/attribution_source?ad_id=..."></script>

您可以选择为 attributionsrc 指定网址值,方法与为点击指定网址值相同;也就是说,对于图片或脚本,您可以按如下方式设置 attributionsrc 网址:

使用单个网址:

<img attributionsrc="https://adtech.example/attribution_source?ad_id=123">

如果提供网址列表:

<img attributionsrc="https://a.example/register-source
  https://b.example/register-source">

使用 fetch()XMLHttpRequest()

此代码可有效模拟使用 attributionsrc 的 HTML 请求会执行的操作:

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

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

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

第 2 步:通过标题(点击次数和观看次数)进行响应

无论是点击还是观看,下一步都是使用 Attribution-Reporting-Register-Source 标头进行响应。

如需了解详情,请查看示例代码

在服务器上收到浏览器请求后,进行响应并在响应中包含 Attribution-Reporting-Register-Source 标头。

res.set(
  "Attribution-Reporting-Register-Source",
  JSON.stringify({
    // Use source_event_id to map it to any granular information
    // you need at ad-serving time
    source_event_id: "412444888111012",
    destination: "https://advertiser.example",
    // Optional fields
    expiry: "604800",
    priority: "100",
    debug_key: "122939999"
  })
);

字符串化后,您的标头如下所示:

{"source_event_id":"412444888111012","destination":"https://advertiser.example","expiry":"604800","priority":"100","debug_key":"122939999"}

后续步骤

了解如何注册归因触发器