PartnerLinkAttribution.sys.mjs (2591B)
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/. */ 4 5 const lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(lazy, { 8 Region: "resource://gre/modules/Region.sys.mjs", 9 }); 10 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", 17 }; 18 19 export var PartnerLinkAttribution = { 20 /** 21 * Sends an attribution request to an anonymizing proxy. 22 * 23 * @param {object} options 24 * @param {string} options.targetURL 25 * The URL we are routing through the anonmyzing proxy. 26 * @param {string} options.source 27 * The source of the anonmized request ("newtab" or "urlbar"). 28 * @param {string} [options.campaignID] 29 * The campaign ID for attribution. This should be a valid path on the 30 * anonymizing proxy. For example, if `campaignID` was `foo`, we'd send an 31 * attribution request to https://topsites.mozilla.com/cid/foo. 32 * Optional. If it's not provided, we default to the topsites campaign. 33 */ 34 async makeRequest({ targetURL, source, campaignID }) { 35 let partner = targetURL.match(/^https?:\/\/(?:www.)?([^.]*)/)[1]; 36 37 let extra = { value: partner }; 38 if (source == "newtab") { 39 Glean.partnerLink.clickNewtab.record(extra); 40 } else if (source == "urlbar") { 41 Glean.partnerLink.clickUrlbar.record(extra); 42 } 43 44 let attributionUrl = Services.prefs.getStringPref( 45 "browser.partnerlink.attributionURL" 46 ); 47 if (!attributionUrl) { 48 Glean.partnerLink.attributionAbort.record(extra); 49 return; 50 } 51 52 // The default campaign is topsites. 53 if (!campaignID) { 54 campaignID = Services.prefs.getStringPref( 55 "browser.partnerlink.campaign.topsites" 56 ); 57 } 58 attributionUrl = attributionUrl + campaignID; 59 let result = await sendRequest(attributionUrl, source, targetURL); 60 if (result) { 61 Glean.partnerLink.attributionSuccess.record(extra); 62 } else { 63 Glean.partnerLink.attributionFailure.record(extra); 64 } 65 }, 66 }; 67 68 async function sendRequest(attributionUrl, source, targetURL) { 69 const request = new Request(attributionUrl); 70 request.headers.set("X-Region", lazy.Region.home); 71 request.headers.set("X-Source", source); 72 request.headers.set("X-Target-URL", targetURL); 73 const response = await fetch(request); 74 return response.ok; 75 }