Bug 1890689 Don't pretend to pre-buffer with DynamicResampler r=pehrsons
[gecko.git] / browser / modules / PartnerLinkAttribution.sys.mjs
blob4f0c7023965119021e0c1c9e988e058d95a4febe
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   Region: "resource://gre/modules/Region.sys.mjs",
9 });
11 export const CONTEXTUAL_SERVICES_PING_TYPES = {
12   TOPSITES_IMPRESSION: "topsites-impression",
13   TOPSITES_SELECTION: "topsites-click",
14   QS_BLOCK: "quicksuggest-block",
15   QS_IMPRESSION: "quicksuggest-impression",
16   QS_SELECTION: "quicksuggest-click",
19 export var PartnerLinkAttribution = {
20   /**
21    * Sends an attribution request to an anonymizing proxy.
22    *
23    * @param {string} targetURL
24    *   The URL we are routing through the anonmyzing proxy.
25    * @param {string} source
26    *   The source of the anonmized request, e.g. "urlbar".
27    * @param {string} [campaignID]
28    *   The campaign ID for attribution. This should be a valid path on the
29    *   anonymizing proxy. For example, if `campaignID` was `foo`, we'd send an
30    *   attribution request to https://topsites.mozilla.com/cid/foo.
31    *   Optional. If it's not provided, we default to the topsites campaign.
32    */
33   async makeRequest({ targetURL, source, campaignID }) {
34     let partner = targetURL.match(/^https?:\/\/(?:www.)?([^.]*)/)[1];
36     function record(method, objectString) {
37       recordTelemetryEvent({
38         method,
39         objectString,
40         value: partner,
41       });
42     }
43     record("click", source);
45     let attributionUrl = Services.prefs.getStringPref(
46       "browser.partnerlink.attributionURL"
47     );
48     if (!attributionUrl) {
49       record("attribution", "abort");
50       return;
51     }
53     // The default campaign is topsites.
54     if (!campaignID) {
55       campaignID = Services.prefs.getStringPref(
56         "browser.partnerlink.campaign.topsites"
57       );
58     }
59     attributionUrl = attributionUrl + campaignID;
60     let result = await sendRequest(attributionUrl, source, targetURL);
61     record("attribution", result ? "success" : "failure");
62   },
65 async function sendRequest(attributionUrl, source, targetURL) {
66   const request = new Request(attributionUrl);
67   request.headers.set("X-Region", lazy.Region.home);
68   request.headers.set("X-Source", source);
69   request.headers.set("X-Target-URL", targetURL);
70   const response = await fetch(request);
71   return response.ok;
74 function recordTelemetryEvent({ method, objectString, value }) {
75   Services.telemetry.setEventRecordingEnabled("partner_link", true);
76   Services.telemetry.recordEvent("partner_link", method, objectString, value);