From cd5e330098073b40b8fe245410ca8758dface586 Mon Sep 17 00:00:00 2001
From: Mike Heneghan <mike@opensystemslab.io>
Date: Thu, 12 Oct 2023 12:30:39 +0100
Subject: [PATCH 1/3] feat: track notice reset click in analytics_log

---
 .../src/@planx/components/Notice/Public.tsx   | 10 ++++++-
 .../FlowEditor/lib/analyticsProvider.tsx      | 28 +++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/editor.planx.uk/src/@planx/components/Notice/Public.tsx b/editor.planx.uk/src/@planx/components/Notice/Public.tsx
index a6ce0f753a..79b6e11d84 100644
--- a/editor.planx.uk/src/@planx/components/Notice/Public.tsx
+++ b/editor.planx.uk/src/@planx/components/Notice/Public.tsx
@@ -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";
@@ -75,6 +76,13 @@ const NoticeComponent: React.FC<Props> = (props) => {
     ? () => props.handleSubmit?.()
     : undefined;
 
+  const { trackNoticeResetClick } = useAnalyticsTracking();
+
+  const handleResetClick = () => {
+    trackNoticeResetClick({'hasClickedNoticeReset': true})
+    props.resetPreview && props.resetPreview()
+  }
+
   return (
     <Card handleSubmit={handleSubmit} isValid>
       <>
@@ -105,7 +113,7 @@ const NoticeComponent: React.FC<Props> = (props) => {
             variant="contained"
             size="large"
             type="submit"
-            onClick={props.resetPreview}
+            onClick={handleResetClick}
             sx={contentFlowSpacing}
           >
             Back to start
diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
index 3aef3a108f..23062a20be 100644
--- a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
+++ b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
@@ -11,6 +11,7 @@ type AnalyticsLogDirection = AnalyticsType | "forwards" | "backwards";
 
 export type HelpClickMetadata = Record<string, string>;
 export type SelectedUrlsMetadata = Record<'selectedUrls', string[]>;
+export type ResetClickMetadata = Record<'hasClickedNoticeReset', boolean>;
 
 let lastAnalyticsLogId: number | undefined = undefined;
 
@@ -18,11 +19,13 @@ const analyticsContext = createContext<{
   createAnalytics: (type: AnalyticsType) => Promise<void>;
   trackHelpClick: (metadata?: HelpClickMetadata) => Promise<void>;
   trackNextStepsLinkClick: (metadata?: SelectedUrlsMetadata) => Promise<void>;
+  trackNoticeResetClick: (metadata?: ResetClickMetadata) =>Promise<void>;
   node: Store.node | null;
 }>({
   createAnalytics: () => Promise.resolve(),
   trackHelpClick: () => Promise.resolve(),
   trackNextStepsLinkClick: () => Promise.resolve(),
+  trackNoticeResetClick: () => Promise.resolve(),
   node: null,
 });
 const { Provider } = analyticsContext;
@@ -104,6 +107,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
         createAnalytics,
         trackHelpClick,
         trackNextStepsLinkClick,
+        trackNoticeResetClick,
         node,
       }}
     >
@@ -199,6 +203,30 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
     }
   }
 
+  async function trackNoticeResetClick(metadata?: ResetClickMetadata) {
+    if (shouldTrackAnalytics && lastAnalyticsLogId) {
+      await publicClient.mutate({
+        mutation: gql`
+          mutation UpdateNoticeResetEnabled(
+            $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 response = await publicClient.mutate({

From 4cb71eb232839bea91c5affda5906fb250327304 Mon Sep 17 00:00:00 2001
From: Mike Heneghan <mike@opensystemslab.io>
Date: Thu, 12 Oct 2023 15:59:27 +0100
Subject: [PATCH 2/3] refactor: update mutation name for improved clarity /
 consistency

---
 editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
index 23062a20be..c5657c07dc 100644
--- a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
+++ b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
@@ -207,7 +207,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
     if (shouldTrackAnalytics && lastAnalyticsLogId) {
       await publicClient.mutate({
         mutation: gql`
-          mutation UpdateNoticeResetEnabled(
+          mutation UpdateHasClickedNoticeReset(
             $id: bigint!,
             $metadata: jsonb = {}
           ) {

From c1f9042fb01fd8220ce40f1cda3d2e1dbd287fe8 Mon Sep 17 00:00:00 2001
From: Mike Heneghan <mike@opensystemslab.io>
Date: Thu, 12 Oct 2023 16:00:25 +0100
Subject: [PATCH 3/3] refactor: update name for unrelated mutation for clarity
 / consistency

---
 editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
index c5657c07dc..2ac9ba50dc 100644
--- a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
+++ b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
@@ -183,7 +183,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
     if (shouldTrackAnalytics && lastAnalyticsLogId) {
       await publicClient.mutate({
         mutation: gql`
-          mutation UpdateHasClickNextStepsLink(
+          mutation UpdateHasClickedNextStepsLink(
             $id: bigint!
             $metadata: jsonb = {}
           ) {