From 866fea83d4dafc6fbb85b81e9476b8794318cd9d Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Fri, 18 Oct 2024 10:48:03 +0000 Subject: [PATCH 1/5] fix(glean): send external-link ping on all external links, always https://mozilla-hub.atlassian.net/browse/MP-1631 --- client/src/telemetry/glean-context.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client/src/telemetry/glean-context.tsx b/client/src/telemetry/glean-context.tsx index cee4ec3c2f4a..6f712f54dd01 100644 --- a/client/src/telemetry/glean-context.tsx +++ b/client/src/telemetry/glean-context.tsx @@ -174,8 +174,12 @@ function handleLinkClick(ev: MouseEvent, click: (source: string) => void) { if (anchor instanceof HTMLAnchorElement) { if (anchor.dataset.glean) { click(anchor.dataset.glean); - } else if (anchor.classList.contains("external")) { - click(`external-link: ${anchor.getAttribute("href") || ""}`); + } + if (anchor.href) { + const url = new URL(anchor.href, document.location.href); + if (url.origin !== document.location.origin) { + click(`external-link: ${url.href}`); + } } } } From 7bb3e1005e33c3edfe3dddf6c916ccb9b7432a47 Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Fri, 18 Oct 2024 11:32:07 +0000 Subject: [PATCH 2/5] wip(glean): review updates --- client/src/telemetry/glean-context.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/client/src/telemetry/glean-context.tsx b/client/src/telemetry/glean-context.tsx index 6f712f54dd01..e11f66cfdfb8 100644 --- a/client/src/telemetry/glean-context.tsx +++ b/client/src/telemetry/glean-context.tsx @@ -176,10 +176,13 @@ function handleLinkClick(ev: MouseEvent, click: (source: string) => void) { click(anchor.dataset.glean); } if (anchor.href) { - const url = new URL(anchor.href, document.location.href); - if (url.origin !== document.location.origin) { - click(`external-link: ${url.href}`); - } + try { + const url = new URL(anchor.href, document.location.href); + if (url.origin !== document.location.origin) { + // use normalized href from `url`: + click(`external-link: ${url.href}`); + } + } catch {} } } } From 99516d16b40ad64213bb39610e7cecd6e313de94 Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Fri, 18 Oct 2024 11:34:31 +0000 Subject: [PATCH 3/5] wip(glean): send invalid-link ping --- client/src/telemetry/glean-context.tsx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/client/src/telemetry/glean-context.tsx b/client/src/telemetry/glean-context.tsx index e11f66cfdfb8..f5792a1819e3 100644 --- a/client/src/telemetry/glean-context.tsx +++ b/client/src/telemetry/glean-context.tsx @@ -176,13 +176,16 @@ function handleLinkClick(ev: MouseEvent, click: (source: string) => void) { click(anchor.dataset.glean); } if (anchor.href) { + let url: URL | undefined; try { - const url = new URL(anchor.href, document.location.href); - if (url.origin !== document.location.origin) { - // use normalized href from `url`: - click(`external-link: ${url.href}`); - } - } catch {} + url = new URL(anchor.href, document.location.href); + } catch { + click(`invalid-link: ${anchor.href}`); + } + if (url && url.origin !== document.location.origin) { + // use normalized href from `url`: + click(`external-link: ${url.href}`); + } } } } From 9af9c52ddb5bdd628a2eaf4a3110eb9f386d0369 Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Mon, 21 Oct 2024 17:02:18 +0000 Subject: [PATCH 4/5] wip(telemetry): simplify logic --- client/src/telemetry/glean-context.tsx | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/client/src/telemetry/glean-context.tsx b/client/src/telemetry/glean-context.tsx index f5792a1819e3..c9af16cf8808 100644 --- a/client/src/telemetry/glean-context.tsx +++ b/client/src/telemetry/glean-context.tsx @@ -175,17 +175,12 @@ function handleLinkClick(ev: MouseEvent, click: (source: string) => void) { if (anchor.dataset.glean) { click(anchor.dataset.glean); } - if (anchor.href) { - let url: URL | undefined; - try { - url = new URL(anchor.href, document.location.href); - } catch { - click(`invalid-link: ${anchor.href}`); - } - if (url && url.origin !== document.location.origin) { - // use normalized href from `url`: - click(`external-link: ${url.href}`); - } + if ( + anchor.href && + anchor.origin && + anchor.origin !== document.location.origin + ) { + click(`external-link: ${anchor.href}`); } } } From 5d4e3f41e6244a5ba40ae374573d55f0b45594f3 Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Mon, 21 Oct 2024 17:07:48 +0000 Subject: [PATCH 5/5] wip(telemetry): extract external-link to constant --- client/src/telemetry/constants.ts | 2 ++ client/src/telemetry/glean-context.tsx | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/client/src/telemetry/constants.ts b/client/src/telemetry/constants.ts index 89c8ee0ce0b9..69befe4dfcec 100644 --- a/client/src/telemetry/constants.ts +++ b/client/src/telemetry/constants.ts @@ -86,3 +86,5 @@ export const LANGUAGE_REMEMBER = "language_remember"; export const THEME_SWITCHER = "theme_switcher"; export const SURVEY = "survey"; export const HOMEPAGE_HERO = "homepage_hero"; + +export const EXTERNAL_LINK = "external-link"; diff --git a/client/src/telemetry/glean-context.tsx b/client/src/telemetry/glean-context.tsx index c9af16cf8808..599d77f09260 100644 --- a/client/src/telemetry/glean-context.tsx +++ b/client/src/telemetry/glean-context.tsx @@ -15,7 +15,7 @@ import { useEffect, useRef } from "react"; import { useLocation } from "react-router"; import { useUserData } from "../user-context"; import { handleSidebarClick } from "./sidebar-click"; -import { VIEWPORT_BREAKPOINTS } from "./constants"; +import { EXTERNAL_LINK, VIEWPORT_BREAKPOINTS } from "./constants"; import { Doc } from "../../../libs/types/document"; export type ViewportBreakpoint = "xs" | "sm" | "md" | "lg" | "xl" | "xxl"; @@ -180,7 +180,7 @@ function handleLinkClick(ev: MouseEvent, click: (source: string) => void) { anchor.origin && anchor.origin !== document.location.origin ) { - click(`external-link: ${anchor.href}`); + click(`${EXTERNAL_LINK}: ${anchor.href}`); } } }