1 import { TidyURL } from '@protontech/tidy-url';
3 import { captureMessage } from '@proton/shared/lib/helpers/sentry';
4 import type { MessageUTMTracker } from '@proton/shared/lib/models/mailUtmTrackers';
6 export const getUTMTrackersFromURL = (originalURL: string) => {
7 // originalURL can be an mailto: link
9 // Only parse http(s) URLs
11 // new URL(originalURL) may crash if the URL is invalid on Safari
12 const { protocol } = new URL(originalURL);
14 if (!protocol.includes('http')) {
22 * Allow amp links. By default, they are being cleaned but too many links were broken.
24 * - https://something.me/L0/https:%2F%2Fsomething-else.amazonaws.com%randomID
26 * - https://something-else.amazonaws.com/randomID
27 * Which was not working as expected when being opened.
29 TidyURL.config.allowAMP = true;
31 const { url, info } = TidyURL.clean(originalURL);
33 If we found removed elements or there is a difference between original link and cleaned link,
34 we have a tracker that we need to display in the privacy dropdown.
35 The original URL might also contain uppercase letters in the first part of the URL that will be updated by the library
36 e.g. https://Proton.me would become https://proton.me, and we don't want it to be detected as a tracker
38 if (originalURL.toLowerCase() !== url.toLowerCase()) {
39 const utmTracker: MessageUTMTracker = {
42 removed: info.removed,
45 return { url, info, utmTracker };
48 return { url: originalURL, info, utmTracker: undefined };
50 captureMessage('Failed to parse URL with trackers');