Skip to content

Commit

Permalink
fix(telemetry): attach global click handlers in app
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Dec 3, 2024
1 parent 42d5a17 commit 9c23ef4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
3 changes: 2 additions & 1 deletion client/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Community } from "./community";
import { ContributorSpotlight } from "./contributor-spotlight";
import { useIsServer, usePing } from "./hooks";

import { useGleanPage } from "./telemetry/glean-context";
import { useGleanClicks, useGleanPage } from "./telemetry/glean-context";
import { MainContentContainer } from "./ui/atoms/page-content";
import { Loading } from "./ui/atoms/loading";
import { Advertising } from "./advertising";
Expand Down Expand Up @@ -142,6 +142,7 @@ export function App(appProps: HydrationData) {

usePing();
useGleanPage(pageNotFound, appProps.doc);
useGleanClicks();
useScrollDepthMeasurement();

const localeMatch = useMatch("/:locale/*");
Expand Down
39 changes: 17 additions & 22 deletions client/src/telemetry/glean-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ function glean(): GleanAnalytics {
const gleanAnalytics = glean();
const GleanContext = React.createContext(gleanAnalytics);

export function useGleanClicks() {
const gleanClick = useGleanClick();

useEffect(() => {
const handler = (ev) => {
handleLinkClick(ev, gleanClick);
handleButtonClick(ev, gleanClick);
handleSidebarClick(ev, gleanClick);
};

window.addEventListener("click", handler);

return () => window.removeEventListener("click", handler);
});
}

function handleButtonClick(ev: MouseEvent, click: (source: string) => void) {
const target = ev.composedPath()?.[0] || ev.target;
const button = (target as HTMLElement | null)?.closest("button");
Expand Down Expand Up @@ -186,28 +202,7 @@ export function GleanProvider(props: { children: React.ReactNode }) {
}

export function useGlean() {
const glean = React.useContext(GleanContext);
const userData = useUserData();

useEffect(() => {
const gleanClick = (source: string) => {
glean.click(getPageProps(userData), {
source,
});
};

const handler = (ev) => {
handleLinkClick(ev, gleanClick);
handleButtonClick(ev, gleanClick);
handleSidebarClick(ev, gleanClick);
};

window.addEventListener("click", handler);

return () => window.removeEventListener("click", handler);
});

return glean;
return React.useContext(GleanContext);
}

function getPageProps(
Expand Down

0 comments on commit 9c23ef4

Please sign in to comment.