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 user resetting flow #2307

Merged
merged 6 commits into from
Oct 18, 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
10 changes: 9 additions & 1 deletion editor.planx.uk/src/@planx/components/Notice/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Card from "@planx/components/shared/Preview/Card";
import { contentFlowSpacing } from "@planx/components/shared/Preview/Card";
import QuestionHeader from "@planx/components/shared/Preview/QuestionHeader";
import { PublicProps } from "@planx/components/ui";
import { useAnalyticsTracking } from "pages/FlowEditor/lib/analyticsProvider";
import React from "react";
import { getContrastTextColor } from "styleUtils";
import { FONT_WEIGHT_SEMI_BOLD } from "theme";
Expand Down Expand Up @@ -75,6 +76,13 @@ const NoticeComponent: React.FC<Props> = (props) => {
? () => props.handleSubmit?.()
: undefined;

const { trackResetFlow } = useAnalyticsTracking();

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

return (
<Card handleSubmit={handleSubmit} isValid>
<>
Expand Down Expand Up @@ -105,7 +113,7 @@ const NoticeComponent: React.FC<Props> = (props) => {
variant="contained"
size="large"
type="submit"
onClick={props.resetPreview}
onClick={handleNoticeResetClick}
sx={contentFlowSpacing}
>
Back to start
Expand Down
3 changes: 3 additions & 0 deletions editor.planx.uk/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,16 @@ const PublicToolbar: React.FC<{
const showCentredServiceTitle = useMediaQuery((theme: Theme) =>
theme.breakpoints.up("md"),
);

const { trackResetFlow } = useAnalyticsTracking();

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

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

export type HelpClickMetadata = Record<string, string>;
export type SelectedUrlsMetadata = Record<'selectedUrls', string[]>;
export type SelectedUrlsMetadata = Record<"selectedUrls", string[]>;

let lastAnalyticsLogId: number | undefined = undefined;

const analyticsContext = createContext<{
createAnalytics: (type: AnalyticsType) => Promise<void>;
trackHelpClick: (metadata?: HelpClickMetadata) => Promise<void>;
trackNextStepsLinkClick: (metadata?: SelectedUrlsMetadata) => Promise<void>;
trackResetFlow: () => Promise<void>;
node: Store.node | null;
}>({
createAnalytics: () => Promise.resolve(),
trackHelpClick: () => Promise.resolve(),
trackNextStepsLinkClick: () => Promise.resolve(),
trackResetFlow: () => Promise.resolve(),
node: null,
});
const { Provider } = analyticsContext;
Expand Down Expand Up @@ -104,6 +106,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
createAnalytics,
trackHelpClick,
trackNextStepsLinkClick,
trackResetFlow,
node,
}}
>
Expand Down Expand Up @@ -199,6 +202,27 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}
}

async function trackResetFlow() {
if (shouldTrackAnalytics && lastAnalyticsLogId) {
await publicClient.mutate({
mutation: gql`
mutation UpdateHasResetFlow($id: bigint!, $flow_direction: String) {
update_analytics_logs_by_pk(
pk_columns: { id: $id }
_set: { flow_direction: $flow_direction }
) {
id
}
}
`,
variables: {
id: lastAnalyticsLogId,
flow_direction: "reset",
},
});
}
}

async function createAnalytics(type: AnalyticsType) {
if (shouldTrackAnalytics) {
const response = await publicClient.mutate({
Expand Down
1 change: 1 addition & 0 deletions hasura.planx.uk/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- role: public
permission:
columns:
- flow_direction
jessicamcinchak marked this conversation as resolved.
Show resolved Hide resolved
- has_clicked_help
- metadata
filter: {}
Expand Down