了解如何注册来源,以将点击和观看归因于适当的事件。
归因来源是指与广告相关的事件(点击或观看),广告技术平台可以为其附加以下类型的信息:
- 内容相关报告数据,例如广告素材 ID、广告系列或地理位置相关信息。
- 转化目标,例如您希望用户完成转化的网站。
按照本文档中的步骤操作,您可以注册来源(广告展示或点击),然后浏览器会将转化归因于这些来源。
注册方法
如需注册归因来源,请使用 HTML 元素或 JavaScript 调用:
<a>
标记<img>
标记<script>
标记fetch
次来电XMLHttpRequest
window.open
次来电
这会生成网络请求,您可以使用来源注册 HTTP 响应标头对其做出响应。
注册点击或观看的来源
如需注册点击或观看的归因来源,请按照此处列出的步骤操作。请按照完整步骤操作。摘要如下:
- 发起来源注册。使用 HTML 元素或 JavaScript 调用发出请求。如您将在以下部分中看到,对于点击和观看,此步骤有所不同。
使用来源注册标头进行响应,完成来源注册。收到该请求后,请使用标头
Attribution-Reporting-Register-Source
进行响应。在该标头中,指定所选的归因报告配置。无论是点击次数还是观看次数,此步骤都是相同的。摘要报告示例:
{ "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 调用以注册来源时,您可能需要使用 attributionsrc
或 attributionReporting
。如需详细了解何时需要提供这些信息,请参阅下表。
如果 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"}
后续步骤
了解如何注册归因触发器。