注册归因来源

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

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

  • 内容相关报告数据,例如广告素材 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 ensure 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"}

后续步骤

不妨了解如何注册归因触发器