Skip to content

Commit

Permalink
refactor(telemetry): extract getPageProps()
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Dec 3, 2024
1 parent 10e7b33 commit bca2cf0
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions client/src/telemetry/glean-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "../env";
import { useEffect, useRef } from "react";
import { useLocation } from "react-router";
import { useUserData } from "../user-context";
import { UserData, useUserData } from "../user-context";
import { handleSidebarClick } from "./sidebar-click";
import { EXTERNAL_LINK, VIEWPORT_BREAKPOINTS } from "./constants";
import { Doc } from "../../../libs/types/document";
Expand Down Expand Up @@ -201,31 +201,46 @@ export function useGlean() {
return React.useContext(GleanContext);
}

function getPageProps(
userData: UserData,
{
pageNotFound,
isBaseline,
}: { pageNotFound?: boolean; isBaseline?: "high" | "low" | false } = {}
): PageProps {
return {
path: window?.location.toString(),
referrer: document?.referrer,
// on port 3000 this will always return "200":
httpStatus: pageNotFound ? "404" : "200",
userLanguages: Array.from(navigator?.languages || []),
geo: userData?.geo?.country,
geo_iso: userData?.geo?.country_iso,
subscriptionType: userData?.subscriptionType || "anonymous",
viewportBreakpoint: VIEWPORT_BREAKPOINTS.find(
([_, width]) => width <= window.innerWidth
)?.[0],
isBaseline: isBaseline
? `baseline_${isBaseline}`
: isBaseline === false
? "not_baseline"
: undefined,
utm: getUTMParameters(),
};
}

export function useGleanPage(pageNotFound: boolean, doc?: Doc) {
const loc = useLocation();
const userData = useUserData();
const path = useRef<String | null>(null);

return useEffect(() => {
const submit = gleanAnalytics.page({
path: window?.location.toString(),
referrer: document?.referrer,
// on port 3000 this will always return "200":
httpStatus: pageNotFound ? "404" : "200",
userLanguages: Array.from(navigator?.languages || []),
geo: userData?.geo?.country,
geo_iso: userData?.geo?.country_iso,
subscriptionType: userData?.subscriptionType || "anonymous",
viewportBreakpoint: VIEWPORT_BREAKPOINTS.find(
([_, width]) => width <= window.innerWidth
)?.[0],
isBaseline: doc?.baseline?.baseline
? `baseline_${doc.baseline.baseline}`
: doc?.baseline?.baseline === false
? "not_baseline"
: undefined,
utm: getUTMParameters(),
});
const submit = gleanAnalytics.page(
getPageProps(userData, {
pageNotFound,
isBaseline: doc?.baseline?.baseline,
})
);
if (typeof userData !== "undefined" && path.current !== loc.pathname) {
path.current = loc.pathname;
submit();
Expand Down

0 comments on commit bca2cf0

Please sign in to comment.