Skip to content

Commit

Permalink
feat: track a user hitting the "Save" application link (#2355)
Browse files Browse the repository at this point in the history
* feat: track a user hitting the "Save" application link

- When a user decided to click the "Save" application link update the analytics_log for the node with a flow_direction of "save"
- This should allow us to analyse which nodes are most saved on etc.
  • Loading branch information
Mike-Heneghan authored Oct 31, 2023
1 parent 723a0e7 commit 51c1b32
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions editor.planx.uk/src/@planx/components/Notice/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ const NoticeComponent: React.FC<Props> = (props) => {
? () => props.handleSubmit?.()
: undefined;

const { trackResetFlow } = useAnalyticsTracking();
const { trackFlowDirectionChange } = useAnalyticsTracking();

const handleNoticeResetClick = () => {
trackResetFlow();
trackFlowDirectionChange("reset");
props.resetPreview && props.resetPreview();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Box from "@mui/material/Box";
import Link from "@mui/material/Link";
import { styled } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import { useAnalyticsTracking } from "pages/FlowEditor/lib/analyticsProvider";
import { useStore } from "pages/FlowEditor/lib/store";
import React from "react";
import { ApplicationPath } from "types";
Expand All @@ -16,10 +17,18 @@ const InnerContainer = styled(Box)(({ theme }) => ({

const SaveResumeButton: React.FC = () => {
const saveToEmail = useStore((state) => state.saveToEmail);
const onClick = () =>
useStore.setState({
path: saveToEmail ? ApplicationPath.Save : ApplicationPath.Resume,
});
const { trackFlowDirectionChange } = useAnalyticsTracking();

const handleClick = () => {
if (saveToEmail) {
trackFlowDirectionChange("save");
useStore.setState({ path: ApplicationPath.Save });
} else {
useStore.setState({ path: ApplicationPath.Resume });
}
};

const onClick = () => handleClick();

return (
<InnerContainer>
Expand Down
4 changes: 2 additions & 2 deletions editor.planx.uk/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ const PublicToolbar: React.FC<{
theme.breakpoints.up("md"),
);

const { trackResetFlow } = useAnalyticsTracking();
const { trackFlowDirectionChange } = useAnalyticsTracking();

const handleRestart = async () => {
if (
confirm(
"Are you sure you want to restart? This will delete your previous answers",
)
) {
trackResetFlow();
trackFlowDirectionChange("reset");
if (path === ApplicationPath.SingleSession) {
clearLocalFlow(id);
window.location.reload();
Expand Down
23 changes: 16 additions & 7 deletions editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import React, { createContext, useContext, useEffect, useState } from "react";
import { Store, useStore } from "./store";

export type AnalyticsType = "init" | "resume";
type AnalyticsLogDirection = AnalyticsType | "forwards" | "backwards" | "reset";
type AnalyticsLogDirection =
| AnalyticsType
| "forwards"
| "backwards"
| "reset"
| "save";

export type HelpClickMetadata = Record<string, string>;
export type SelectedUrlsMetadata = Record<"selectedUrls", string[]>;
Expand All @@ -32,13 +37,15 @@ const analyticsContext = createContext<{
createAnalytics: (type: AnalyticsType) => Promise<void>;
trackHelpClick: (metadata?: HelpClickMetadata) => Promise<void>;
trackNextStepsLinkClick: (metadata?: SelectedUrlsMetadata) => Promise<void>;
trackResetFlow: () => Promise<void>;
trackFlowDirectionChange: (
flowDirection: AnalyticsLogDirection,
) => Promise<void>;
node: Store.node | null;
}>({
createAnalytics: () => Promise.resolve(),
trackHelpClick: () => Promise.resolve(),
trackNextStepsLinkClick: () => Promise.resolve(),
trackResetFlow: () => Promise.resolve(),
trackFlowDirectionChange: () => Promise.resolve(),
node: null,
});
const { Provider } = analyticsContext;
Expand Down Expand Up @@ -120,7 +127,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
createAnalytics,
trackHelpClick,
trackNextStepsLinkClick,
trackResetFlow,
trackFlowDirectionChange,
node,
}}
>
Expand Down Expand Up @@ -266,11 +273,13 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}
}

async function trackResetFlow() {
async function trackFlowDirectionChange(
flowDirection: AnalyticsLogDirection,
) {
if (shouldTrackAnalytics && lastAnalyticsLogId) {
await publicClient.mutate({
mutation: gql`
mutation UpdateHasResetFlow($id: bigint!, $flow_direction: String) {
mutation UpdateFlowDirection($id: bigint!, $flow_direction: String) {
update_analytics_logs_by_pk(
pk_columns: { id: $id }
_set: { flow_direction: $flow_direction }
Expand All @@ -281,7 +290,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
`,
variables: {
id: lastAnalyticsLogId,
flow_direction: "reset",
flow_direction: flowDirection,
},
});
}
Expand Down

0 comments on commit 51c1b32

Please sign in to comment.