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: track a user navigating backwards through flow by node id #2357

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Link from "@mui/material/Link";
import { styled } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import { visuallyHidden } from "@mui/utils";
import { useAnalyticsTracking } from "pages/FlowEditor/lib/analyticsProvider";
import { useStore } from "pages/FlowEditor/lib/store";
import React from "react";
import Caret from "ui/icons/Caret";
Expand Down Expand Up @@ -140,6 +141,13 @@ const ResultReason: React.FC<IResultReason> = ({
: ""
}`;

const { trackBackwardsNavigationByNodeId } = useAnalyticsTracking();

const handleChangeAnswer = (id: string) => {
trackBackwardsNavigationByNodeId(id, "change");
changeAnswer(id);
};

return (
<Root>
<StyledAccordion
Expand Down Expand Up @@ -179,7 +187,7 @@ const ResultReason: React.FC<IResultReason> = ({
component="button"
onClick={(event) => {
event.stopPropagation();
changeAnswer(id);
handleChangeAnswer(id);
}}
>
Change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { visuallyHidden } from "@mui/utils";
import { PASSPORT_UPLOAD_KEY } from "@planx/components/DrawBoundary/model";
import { TYPES } from "@planx/components/types";
import format from "date-fns/format";
import { useAnalyticsTracking } from "pages/FlowEditor/lib/analyticsProvider";
import { Store, useStore } from "pages/FlowEditor/lib/store";
import React from "react";
import { FONT_WEIGHT_SEMI_BOLD } from "theme";
Expand Down Expand Up @@ -203,8 +204,11 @@ function SummaryListsBySections(props: SummaryListsBySectionsProps) {
// For applicable component types, display a list of their question & answers with a "change" link
// ref https://design-system.service.gov.uk/components/summary-list/
function SummaryList(props: SummaryListProps) {
const handleClick = (nodeId: string) => {
props.changeAnswer(nodeId);
const { trackBackwardsNavigationByNodeId } = useAnalyticsTracking();

const handleChangeAnswer = (id: string) => {
trackBackwardsNavigationByNodeId(id, "change");
props.changeAnswer(id);
};

return (
Expand All @@ -222,7 +226,7 @@ function SummaryList(props: SummaryListProps) {
<dd>
{props.showChangeButton && (
<Link
onClick={() => handleClick(nodeId)}
onClick={() => handleChangeAnswer(nodeId)}
component="button"
fontSize="body2.fontSize"
>
Expand Down
51 changes: 51 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type AnalyticsLogDirection =

export type HelpClickMetadata = Record<string, string>;
export type SelectedUrlsMetadata = Record<"selectedUrls", string[]>;
export type BackwardsNaviagtionInitiatorType = "change" | "back";
Mike-Heneghan marked this conversation as resolved.
Show resolved Hide resolved

type NodeMetadata = {
flagset?: FlagSet;
Expand All @@ -29,6 +30,8 @@ type NodeMetadata = {
description?: string;
};
flag?: Flag;
title?: string;
type?: TYPES;
};

let lastAnalyticsLogId: number | undefined = undefined;
Expand All @@ -40,12 +43,17 @@ const analyticsContext = createContext<{
trackFlowDirectionChange: (
flowDirection: AnalyticsLogDirection,
) => Promise<void>;
trackBackwardsNavigationByNodeId: (
nodeId: string,
backwardsNavigationType: BackwardsNaviagtionInitiatorType,
) => Promise<void>;
node: Store.node | null;
}>({
createAnalytics: () => Promise.resolve(),
trackHelpClick: () => Promise.resolve(),
trackNextStepsLinkClick: () => Promise.resolve(),
trackFlowDirectionChange: () => Promise.resolve(),
trackBackwardsNavigationByNodeId: () => Promise.resolve(),
node: null,
});
const { Provider } = analyticsContext;
Expand All @@ -64,6 +72,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
resultData,
previewEnvironment,
flowId,
flow,
] = useStore((state) => [
state.currentCard,
state.breadcrumbs,
Expand All @@ -72,6 +81,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
state.resultData,
state.previewEnvironment,
state.id,
state.flow,
]);
const node = currentCard();
const isAnalyticsEnabled =
Expand Down Expand Up @@ -128,6 +138,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
trackHelpClick,
trackNextStepsLinkClick,
trackFlowDirectionChange,
trackBackwardsNavigationByNodeId,
node,
}}
>
Expand Down Expand Up @@ -296,6 +307,37 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}
}

async function trackBackwardsNavigationByNodeId(
nodeId: string,
initiator: BackwardsNaviagtionInitiatorType,
) {
const targetNodeMetadata = getTitleAndTypeFromFlow(nodeId);
const metadata: Record<string, NodeMetadata> = {};
Mike-Heneghan marked this conversation as resolved.
Show resolved Hide resolved
metadata[`${initiator}`] = targetNodeMetadata;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for this syntax as I was getting errors that the initiator wasn't being used when I was trying to apply is a key in the object creation.


if (shouldTrackAnalytics && lastAnalyticsLogId) {
await publicClient.mutate({
mutation: gql`
mutation UpdateHaInitiatedBackwardsNavigation(
$id: bigint!
$metadata: jsonb = {}
) {
update_analytics_logs_by_pk(
pk_columns: { id: $id }
_append: { metadata: $metadata }
) {
id
}
}
`,
variables: {
id: lastAnalyticsLogId,
metadata,
},
});
}
}

async function createAnalytics(type: AnalyticsType) {
if (shouldTrackAnalytics) {
const userAgent = Bowser.parse(window.navigator.userAgent);
Expand Down Expand Up @@ -350,6 +392,15 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
return {};
}
}

function getTitleAndTypeFromFlow(nodeId: string) {
Mike-Heneghan marked this conversation as resolved.
Show resolved Hide resolved
const { data, type } = flow[nodeId];
const nodeMetadata: NodeMetadata = {
title: data?.text,
type: type,
};
return nodeMetadata;
}
};

/**
Expand Down
8 changes: 6 additions & 2 deletions editor.planx.uk/src/pages/Preview/Questions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ const Questions = ({ previewEnvironment }: QuestionsProps) => {
state.setPreviewEnvironment,
]);
const isStandalone = previewEnvironment === "standalone";
const { createAnalytics, node } = useAnalyticsTracking();
const { createAnalytics, node, trackBackwardsNavigationByNodeId } =
useAnalyticsTracking();
const [gotFlow, setGotFlow] = useState(false);
const isUsingLocalStorage =
useStore((state) => state.path) === ApplicationPath.SingleSession;
Expand Down Expand Up @@ -146,7 +147,10 @@ const Questions = ({ previewEnvironment }: QuestionsProps) => {

const goBack = useCallback(() => {
const previous = previousCard(node);
if (previous) record(previous);
if (previous) {
trackBackwardsNavigationByNodeId(previous, "back");
record(previous);
}
}, [node?.id]);

const showBackButton = useMemo(
Expand Down
Loading