Skip to content

Commit

Permalink
feat: track a user clicking the back button
Browse files Browse the repository at this point in the history
- For each node an analytics_log is created
- Add a new coloumn of has_clicked_back which stores a boolean if the user clicks back
- Update the analytics_log for the node where the user clicks back
- Optional metatdata to store where back was in the future if required
  • Loading branch information
Mike-Heneghan committed Nov 1, 2023
1 parent 7d49452 commit 522f9cb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
26 changes: 26 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 BackButtonClickMetadata = Record<string, string>;

type NodeMetadata = {
flagset?: FlagSet;
Expand All @@ -40,12 +41,14 @@ const analyticsContext = createContext<{
trackFlowDirectionChange: (
flowDirection: AnalyticsLogDirection,
) => Promise<void>;
trackBackButtonClick: () => Promise<void>;
node: Store.node | null;
}>({
createAnalytics: () => Promise.resolve(),
trackHelpClick: () => Promise.resolve(),
trackNextStepsLinkClick: () => Promise.resolve(),
trackFlowDirectionChange: () => Promise.resolve(),
trackBackButtonClick: () => Promise.resolve(),
node: null,
});
const { Provider } = analyticsContext;
Expand Down Expand Up @@ -128,6 +131,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
trackHelpClick,
trackNextStepsLinkClick,
trackFlowDirectionChange,
trackBackButtonClick,
node,
}}
>
Expand Down Expand Up @@ -249,6 +253,28 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}
}

async function trackBackButtonClick(metadata?: BackButtonClickMetadata) {
if (shouldTrackAnalytics && lastAnalyticsLogId) {
await publicClient.mutate({
mutation: gql`
mutation UpdateHasClickedBack($id: bigint!, $metadata: jsonb = {}) {
update_analytics_logs_by_pk(
pk_columns: { id: $id }
_set: { has_clicked_back: true }
_append: { metadata: $metadata }
) {
id
}
}
`,
variables: {
id: lastAnalyticsLogId,
metadata,
},
});
}
}

async function trackNextStepsLinkClick(metadata?: SelectedUrlsMetadata) {
if (shouldTrackAnalytics && lastAnalyticsLogId) {
await publicClient.mutate({
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, trackBackButtonClick } =
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) {
trackBackButtonClick();
record(previous);
}
}, [node?.id]);

const showBackButton = useMemo(
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 @@ -48,6 +48,7 @@
permission:
columns:
- flow_direction
- has_clicked_back
- has_clicked_help
- metadata
- next_log_created_at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."analytics_logs" drop column "has_clicked_back";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."analytics_logs" add column "has_clicked_back" boolean
not null default 'false';

0 comments on commit 522f9cb

Please sign in to comment.