Skip to content

Commit

Permalink
fix(telemetry): set page props on click
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Dec 3, 2024
1 parent bca2cf0 commit 8b6de97
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions client/src/telemetry/glean-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ export type PageEventProps = {

export type ElementClickedProps = {
source: string;
subscriptionType: string;
};

export type GleanAnalytics = {
page: (arg: PageProps) => () => void;
click: (arg: ElementClickedProps) => void;
page: (page: PageProps) => () => void;
click: (page: PageProps, element: ElementClickedProps) => void;
};

const FIRST_PARTY_DATA_OPT_OUT_COOKIE_NAME = "moz-1st-party-data-opt-out";
Expand All @@ -79,7 +78,7 @@ function glean(): GleanAnalytics {
//SSR return noop.
return {
page: (page: PageProps) => () => {},
click: (element: ElementClickedProps) => {},
click: (page: PageProps, element: ElementClickedProps) => {},
};
}
const userIsOptedOut = document.cookie
Expand Down Expand Up @@ -137,26 +136,15 @@ function glean(): GleanAnalytics {
setPageProps(page);
return () => pings.page.submit();
},
click: (event: ElementClickedProps) => {
const { source, subscriptionType: subscription_type } = event;
click: (page: PageProps, event: ElementClickedProps) => {
setPageProps(page);
const { source } = event;
elementMetric.clicked.record({
source,
subscription_type,
});
pings.action.submit();
},
};
const gleanClick = (source: string) => {
gleanContext.click({
source,
subscriptionType: "",
});
};
window?.addEventListener("click", (ev) => {
handleLinkClick(ev, gleanClick);
handleButtonClick(ev, gleanClick);
handleSidebarClick(ev, gleanClick);
});

return gleanContext;
}
Expand Down Expand Up @@ -198,7 +186,21 @@ export function GleanProvider(props: { children: React.ReactNode }) {
}

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

const gleanClick = (source: string) => {
glean.click(getPageProps(userData), {
source,
});
};
window?.addEventListener("click", (ev) => {
handleLinkClick(ev, gleanClick);
handleButtonClick(ev, gleanClick);
handleSidebarClick(ev, gleanClick);
});

return glean;
}

function getPageProps(
Expand Down Expand Up @@ -257,12 +259,11 @@ export function useGleanClick() {
console.log({ gleanClick: source });
}

glean.click({
glean.click(getPageProps(userData), {
source,
subscriptionType: userData?.subscriptionType || "none",
});
},
[glean, userData?.subscriptionType]
[glean, userData]
);
}

Expand Down

0 comments on commit 8b6de97

Please sign in to comment.