addUtmParams.mjs (1092B)
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 file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * BASE_PARAMS keys/values can be modified from outside this file 7 */ 8 export const BASE_PARAMS = { 9 utm_source: "activity-stream", 10 utm_campaign: "firstrun", 11 utm_medium: "referral", 12 }; 13 14 /** 15 * Takes in a url as a string or URL object and returns a URL object with the 16 * utm_* parameters added to it. If a URL object is passed in, the paraemeters 17 * are added to it (the return value can be ignored in that case as it's the 18 * same object). 19 */ 20 export function addUtmParams(url, utmTerm) { 21 let returnUrl = url; 22 if (typeof returnUrl === "string") { 23 returnUrl = new URL(url); 24 } 25 for (let [key, value] of Object.entries(BASE_PARAMS)) { 26 if (!returnUrl.searchParams.has(key)) { 27 returnUrl.searchParams.append(key, value); 28 } 29 } 30 if (!returnUrl.searchParams.has("utm_term")) { 31 returnUrl.searchParams.append("utm_term", utmTerm); 32 } 33 return returnUrl; 34 }