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 /* global RPMGetUpdateChannel, RPMGetFxAccountsEndpoint */
7 const PARAMS = new URL(location).searchParams;
8 const ENTRYPOINT = "new-install-page";
9 const SOURCE = `new-install-page-${RPMGetUpdateChannel()}`;
10 const CAMPAIGN = "dedicated-profiles";
11 const ENDPOINT = PARAMS.get("endpoint");
13 function appendAccountsParams(url) {
14 url.searchParams.set("entrypoint", ENTRYPOINT);
15 url.searchParams.set("utm_source", SOURCE);
16 url.searchParams.set("utm_campaign", CAMPAIGN);
19 function appendParams(url, params) {
20 appendAccountsParams(url);
22 for (let [key, value] of Object.entries(params)) {
23 url.searchParams.set(key, value);
27 async function requestFlowMetrics() {
28 let requestURL = new URL(await endpoint);
29 requestURL.pathname = "metrics-flow";
30 appendParams(requestURL, {
34 let response = await fetch(requestURL, { credentials: "omit" });
35 if (response.status === 200) {
36 return response.json();
39 throw new Error(`Failed to retrieve metrics: ${response.status}`);
42 async function submitForm(event) {
43 // We never want to submit the form.
44 event.preventDefault();
46 let input = document.getElementById("sync-input");
48 let { flowId, flowBeginTime } = await metrics;
50 let requestURL = new URL(await endpoint);
51 appendParams(requestURL, {
53 "utm_campaign": CAMPAIGN,
56 "flow_begin_time": flowBeginTime,
59 window.open(requestURL, "_blank", "noopener");
60 document.getElementById("sync").hidden = true;
63 const endpoint = RPMGetFxAccountsEndpoint(ENTRYPOINT);
65 // This must come before the CSP is set or it will be blocked.
66 const metrics = requestFlowMetrics();
68 document.addEventListener("DOMContentLoaded", () => {
69 document.getElementById("sync").addEventListener("submit", submitForm);