瞭解如何登錄來源,將點擊和瀏覽歸因於適當的事件。
歸因來源是廣告相關事件 (點擊或瀏覽),廣告技術可以附加下列類型的資訊:
- 內容報表資料,例如廣告素材 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> 標記 |
(navigation source)attributionsrc 為必要屬性。 |
<img> 標記 |
(事件來源)attributionsrc 為必填。 |
<script> 標記 |
(事件來源)attributionsrc 為必填。 |
fetch 通話 |
attributionReporting 選項為必要。 |
XMLHttpRequest |
attributionReporting 選項為必要。 |
window.open 通話 |
(navigation source)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
而不使用網址。您也可以指定單一網址。此外,您也可以使用以空格分隔的網址清單。
使用網址會導致瀏覽器啟動個別的保活擷取要求 (每個網址一個),其中包含 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"}
後續步驟
瞭解如何註冊歸因觸發條件。