了解如何注册来源以将点击和观看归因于相应的事件。
归因来源是指与广告相关的事件(点击或观看),广告技术平台可向其附加以下类型的信息: - 内容相关数据,例如广告素材 ID、广告系列相关信息或地理位置。 - 转化目标,即您希望用户完成转化的网站。
您可以按照本文档中的步骤记录广告展示或点击数据,浏览器随后就会将转化归因于这些来源。
注册方法
若要注册归因来源,请使用 HTML 元素或 JavaScript 调用:
<a>
标记<img>
标记<script>
标记fetch
个电话素材资源XMLHttpRequest
window.open
这会生成网络请求,您随后会使用来源注册 HTTP 响应标头来响应网络请求。
注册点击或观看来源
若要为点击或观看注册归因来源,请按此处介绍的步骤操作。下面是完整步骤。摘要如下:
- 启动来源注册。使用 HTML 元素或 JavaScript 调用发出请求。此步骤针对点击次数和观看次数有所不同,您在以下几个部分中会注意到。
使用来源注册标头进行响应,完成来源注册。收到该请求后,使用标头
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 调用以注册来源时,您可能需要使用 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">
在 JavaScript 中,如果您为 attributionsrc
指定了值,请务必对网址进行编码,以防网址包含特殊字符(例如“=”),这会导致参数无法正确解析。
请按如下方式编码:
const encodedUrl = encodeURIComponent(
'https://adtech.example/attribution_source?ad_id=...');
window.open(
"https://shoes.example/landing",
"_blank",
attributionsrc=${encodedUrl});
attributionsrc
还可以接受以空格分隔的网址列表,如下面的锚标记所示:
<!-- With an anchor tag -->
<a href=... attributionsrc="https://a.example/register-source
https://b.example/register-source">
或使用 window.open()
将其调到此处。
// With window.open()
window.open('...', '_blank', attributionsrc=${encodedUrl1}
attributionsrc=${encodedUrl2})
在这种情况下,两个网址都会收到 navigation-source-eligible
attributionsrc
请求(包含 Attribution-Reporting-Eligible
标头的请求)。
包含或不包含值的 attributionsrc
如前所述,您可以指定 attributionsrc
而不指定网址。您也可以指定一个网址。此外,仅适用于来源(不适用于触发器),您可以使用以空格分隔的网址列表。
使用网址会导致浏览器发起单独的 keepalive 提取请求(每个网址对应一个),其中包含 Attribution-Reporting-Eligible
请求标头。
如果您想通过响应独立于元素主请求的请求进行来源注册,这会非常有用。
例如,如果您需要注册锚点元素点击的来源,实际上可能无法控制目的地;在这种情况下,您需要一个配置,通过该配置,将来源注册标头作为与导航分开的请求的响应发送,并且您可以完全控制。通过为 attributionsrc
指定显式值,即可指示浏览器发出该额外请求并配置其目的地。
对于注册视图的第 1 步,请转到标签页,然后选择“视图”第 1 步。
如需为视图注册归因来源,您可以使用需要添加 attributionsrc
属性的图片或脚本标记。
或者,您也可以使用 JavaScript fetch()
或 XMLHttpRequest()
。
含图片
<img attributionsrc
src="https://adtech.example/attribution_source?ad_id=..."/>
使用脚本
<script attributionsrc
src="https://adtech.example/attribution_source?ad_id=..."/>
(可选)您可以按照与点击操作相同的方式为 attributionsrc
指定网址值;也就是说,对于图片或脚本,可以按如下所示设置一个或多个 attributionsrc
网址:
使用一个网址:
attributionsrc="https://adtech.example/attribution_source?ad_id=123"
使用网址列表:
attributionsrc="https://a.example/register-source
https://b.example/register-source"
使用 fetch()
或 XMLHttpRequest()
此代码有效地模拟了使用 attributionsrc
的 HTML 请求的作用:
// With fetch
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 });
// With XHR
const attributionReporting = {
eventSourceEligible: true,
triggerEligible: false,
};
const req = new XMLHttpRequest();
req.open('GET', url);
req.setAttributionReporting(
attributionReporting);
req.send();
对于记录点击次数的第 1 步,请转到标签页,然后选择“点击次数”第 1 步。
第 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"}
后续步骤
不妨了解如何注册归因触发器。