A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://developer.mozilla.org/en-US/docs/Web/API/Attribution_Reporting_API/Registering_sources below:

Registering attribution sources - Web APIs

Registering attribution sources

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

This article explains how to register attribution sources when using the Attribution Reporting API.

Basic methodology

Attribution sources take the form of links, images, or scripts contained within content that you want to measure interactions with (for example, they might be ads that you want to measure conversions on). These cause the browser to store source data in a private local cache (accessible only by the browser) when specific user interactions occur. The different attribution source types are registered and signal interactions in different ways — they are differentiated as:

What happens behind the scenes to register sources and retrieve and store the source data is the same in both cases:

  1. When the user interacts with an attribution source, it sends an Attribution-Reporting-Eligible header on a request to the server measuring the interactions (typically the advertiser's server), which indicates that the response is eligible to register a source. For example:

    Attribution-Reporting-Eligible: navigation-source
    
  2. When the server receives a request that includes an Attribution-Reporting-Eligible header, it can include an Attribution-Reporting-Register-Source header along with the response to complete source registration. Its value is a JSON string that provides the information the browser should store about the attribution source that was interacted with. The information included in this header also determines which types of report the browser will generate:

  3. After a successful source registration occurs, the browser stores the provided source data in its private local cache.

Navigation-based attribution sources

Navigation sources are useful for measuring interactions with links — for example, a user may see an ad on a publisher's page, and click it to navigate to the advertiser's page where a conversion will hopefully occur.

There are a couple of different types of navigation-based attribution sources (for example, clicking on an ad) that can be registered — those based on HTML (which use the attributionsrc attribute) and those based on Window.open() calls (which use an attributionsrc window feature).

HTML-based navigation sources

To register a navigation-based attribution source you can add the attributionsrc attribute to an appropriate <a> element, which specifies where the registration request will be sent.

If you leave the attribute value blank, the registration request will be sent to the location being linked to. It is also possible to specify one or more additional URLs inside the value to send the registration request to; see the Specifying URLs inside attributionsrc for more details.

attributionsrc can be added declaratively:

<a href="https://shop.example" attributionsrc target="_blank">
  Click to visit our shop
</a>

Or via the HTMLAnchorElement.attributionSrc property:

const aElem = document.querySelector("a");
aElem.attributionSrc = "";

In this case, the interaction occurs, causing the browser to store the source data associated with the navigation-based attribution source (as provided in the Attribution-Reporting-Register-Source response header) when the user clicks the link and the browser receives the response.

Window.open()-based navigation sources

You can also add the attributionsrc feature keyword to the features property of a Window.open() call. In this example we run it in response to a click event being fired:

elem.addEventListener("click", () => {
  window.open("https://shop.example", "_blank", "attributionsrc");
});

In this case, the interaction occurs and the browser stores the source data when Window.open() is invoked, and the browser receives the response.

Note: When setting up a click event like in the above example, it is advisable to set it on a control where a click is expected, such as a <button> or <a> element. This makes more sense semantically, and is more accessible to both screen reader and keyboard users.

Note: To register an attribution source via open(), it must be called with transient activation (i.e., inside a user interaction event handler such as click) within five seconds of user interaction.

Event-based attribution sources

Event-based attribution sources cause the browser to store source data in response to some kind of event firing, such as the load event in the case of an <img> or <script> element (which use the attributionsrc attribute like we saw above with the <a> element), or a custom event of your choice set in your JavaScript.

HTML-based event sources

HTML-based event sources can be used for measuring interactions with a publisher's page when it first loads — or more precisely when an <img> or <script> loads. To register an event-based attribution source via HTML, you can add the attributionsrc attribute to an appropriate element — <img> or <script>.

If you leave the attribute value blank, the registration request will be sent to the server the requested resource is hosted on. It is also possible to specify one or more additional URLs inside the value to send the registration request to; see Specifying URLs inside attributionsrc for more details.

Let's look at an <img> element example:

<img src="advertising-image.png" attributionsrc />

You could also achieve this via the HTMLImageElement.attributionSrc property:

const imgElem = document.querySelector("img");
imgElem.attributionSrc = "";

The browser stores the attribution source data when the browser receives the response containing the image file (i.e., when the load event occurs). Bear in mind that users might not necessarily be able to perceive the image at all — it might be a 1x1 transparent tracking pixel that is only being used for attribution reporting.

A <script> example might look like so:

<script src="advertising-script.js" attributionsrc></script>

Or via the HTMLScriptElement.attributionSrc property:

const scriptElem = document.querySelector("script");
scriptElem.attributionSrc = "";

In this case, the interaction occurs and the browser stores the source data when the browser receives the response containing the script.

JavaScript-based event sources

Script-based attribution sources are more versatile than HTML-based attribution sources. You can set up a script to initiate a request that is eligible to register an attribution source based on whatever request suits your app. This is a flexible approach, useful when you want to store source data in response to custom interactions, for example, clicking a custom element or submitting a form.

To set up a script-based attribution source, you can either:

In this case, the interaction occurs and the browser stores the source data when the browser receives the response from the fetch request.

Note: The request can be for any resource. It doesn't need to have anything directly to do with the Attribution Reporting API, and can be a request for JSON, plain text, an image blob, or whatever else makes sense for your app.

Specifying URLs inside attributionsrc

So far, in all the examples we have seen, the attributionsrc attribute/feature or attributionSrc property has been left blank, taking the value of an empty string. This is fine if the server that holds the requested resource is the same server that you also want to handle the registration, i.e., receive the Attribution-Reporting-Eligible header and respond with the Attribution-Reporting-Register-Source header.

However, it might be that the requested resource is not on a server you control, or you just want to handle registering the attribution source on a different server. In such cases, you can specify one or more URLs as the value of attributionsrc. When the resource request occurs, the Attribution-Reporting-Eligible header will be sent to the URL(s) specified in attributionsrc in addition to the resource origin; these URLs can then respond with the Attribution-Reporting-Register-Source to register the source.

For example, in the case of an <a> element you could declare the URL(s) in the attributionsrc attribute:

<a
  href="https://shop.example"
  attributionsrc="https://a.example/register-source">
  Click to visit our shop
</a>

Or in JavaScript via the attributionSrc property:

// 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}`;

In the case of a Window.open() call, the different URLs would have to be listed as multiple separate attributionsrc features in the windowFeatures parameter, separated by commas or whitespace:

// 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}`,
  );
});

Note: Specifying multiple URLs means that multiple attribution sources can be registered on the same feature. You might for example have different campaigns that you are trying to measure the success of, which involve generating different reports on different data.

See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4