res.set(
"Attribution-Reporting-Register-Source",
JSON.stringify({
source_event_id: "412444888111012",
destination: "https://advertiser.example",
trigger_data: [0, 1, 2, 3, 4],
trigger_data_matching: "exact",
expiry: "604800",
priority: "100",
debug_key: "122939999",
event_report_window: "86400",
}),
);Registering attribution sources / notebook
Registering attribution sources
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
res.set(
"Attribution-Reporting-Register-Source",
JSON.stringify({
source_event_id: "412444888111012",
destination: "https://advertiser.example",
trigger_data: [0, 1, 2, 3, 4],
trigger_data_matching: "exact",
expiry: "604800",
priority: "100",
debug_key: "122939999",
event_report_window: "86400",
aggregation_keys: {
campaignCounts: "0x159",
geoValue: "0x5",
},
aggregatable_report_window: "86400",
}),
);<a href="https://shop.example" attributionsrc target="_blank">
Click to visit our shop
</a>const aElem = document.querySelector("a");
aElem.attributionSrc = "";elem.addEventListener("click", () => {
window.open("https://shop.example", "_blank", "attributionsrc");
});<img src="advertising-image.png" alt="" attributionsrc />const imgElem = document.querySelector("img");
imgElem.attributionSrc = "";<script src="advertising-script.js" attributionsrc></script>const scriptElem = document.querySelector("script");
scriptElem.attributionSrc = ""; const attributionReporting = {
eventSourceEligible: true,
triggerEligible: false,
};
// Optionally set keepalive to ensure the request outlives the page
function triggerSourceInteraction() {
fetch("https://shop.example/endpoint", {
keepalive: true,
attributionReporting,
});
}
// Associate the interaction trigger with whatever
// event makes sense for your code (does not have to be a
// DOM event/user interaction)
elem.addEventListener("click", triggerSourceInteraction); const attributionReporting = {
eventSourceEligible: true,
triggerEligible: false,
};
function triggerSourceInteraction() {
const req = new XMLHttpRequest();
req.open("GET", "https://shop.example/endpoint");
// Check availability of setAttributionReporting() before calling
if (typeof req.setAttributionReporting === "function") {
req.setAttributionReporting(attributionReporting);
req.send();
} else {
throw new Error("Attribution reporting not available");
// Include recovery code here as appropriate
}
}
// Associate the interaction trigger with whatever
// event makes sense for your code (does not have to be a
// DOM event/user interaction)
elem.addEventListener("click", triggerSourceInteraction);<a
href="https://shop.example"
attributionsrc="https://a.example/register-source">
Click to visit our shop
</a>// encode the URLs in case they contain special characters
// such as '=' that would be improperly parsed.
const encodedUrlA = encodeURIComponent("https://a.example/register-source");
const encodedUrlB = encodeURIComponent("https://b.example/register-source");
const aElem = document.querySelector("a");
aElem.attributionSrc = `${encodedUrlA} ${encodedUrlB}`;// encode the URLs in case they contain special characters
// such as '=' that would be improperly parsed.
const encodedUrlA = encodeURIComponent("https://a.example/register-source");
const encodedUrlB = encodeURIComponent("https://b.example/register-source");
elem.addEventListener("click", () => {
window.open(
"https://ourshop.example",
"_blank",
`attributionsrc=${encodedUrlA},attributionsrc=${encodedUrlB}`,
);
});