Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(telemetry): measure survey actions #11273

Merged
merged 8 commits into from
Jun 7, 2024
1 change: 1 addition & 0 deletions client/src/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ export const BASELINE = Object.freeze({
export const CLIENT_SIDE_NAVIGATION = "client_side_nav";
export const LANGUAGE = "language";
export const THEME_SWITCHER = "theme_switcher";
export const SURVEY = "survey";
37 changes: 28 additions & 9 deletions client/src/ui/molecules/document-survey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useIsServer } from "../../../hooks";
import { Icon } from "../../atoms/icon";
import { useLocation } from "react-router";
import { DEV_MODE, WRITER_MODE } from "../../../env";
import { useGleanClick } from "../../../telemetry/glean-context";
import { SURVEY } from "../../../telemetry/constants";

const FORCE_SURVEY_PREFIX = "#FORCE_SURVEY=";

Expand Down Expand Up @@ -49,6 +51,7 @@ export function DocumentSurvey({ doc }: { doc: Doc }) {
}

function SurveyDisplay({ survey, force }: { survey: Survey; force: boolean }) {
const gleanClick = useGleanClick();
const details = React.useRef<HTMLDetailsElement | null>(null);

const [originalState] = React.useState(() => getSurveyState(survey.bucket));
Expand All @@ -58,14 +61,36 @@ function SurveyDisplay({ survey, force }: { survey: Survey; force: boolean }) {
writeSurveyState(survey.bucket, state);
}, [state, survey.bucket]);

const measure = React.useCallback(
(action: string) => gleanClick(`${SURVEY}: ${action} ${survey.bucket}`),
[gleanClick, survey.bucket]
);

const seen = React.useCallback(() => {
setState((state) => {
if (state.seen_at) {
return state;
}

measure("seen");

return {
...state,
seen_at: Date.now(),
};
});
}, [measure]);

function dismiss() {
measure("dismissed");
setState({
...state,
dismissed_at: Date.now(),
});
}

function submitted() {
measure("submitted");
setState({
...state,
submitted_at: Date.now(),
Expand All @@ -80,6 +105,7 @@ function SurveyDisplay({ survey, force }: { survey: Survey; force: boolean }) {

const listener = () => {
if (current.open && !state.opened_at) {
measure("opened");
setState({
...state,
opened_at: Date.now(),
Expand All @@ -90,16 +116,9 @@ function SurveyDisplay({ survey, force }: { survey: Survey; force: boolean }) {
current.addEventListener("toggle", listener);

return () => current.removeEventListener("toggle", listener);
}, [details, state, survey]);
}, [details, state, survey, measure]);

React.useEffect(() => {
if (!state.seen_at) {
setState({
...state,
seen_at: Date.now(),
});
}
}, [state]);
React.useEffect(seen, [seen]);

React.useEffect(() => {
// For this to work, the Survey needs this JavaScript action:
Expand Down
Loading