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 notice reset click in analytics_log #2301

Closed
wants to merge 3 commits into from
Closed
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 { trackNoticeResetClick } = useAnalyticsTracking();

const handleResetClick = () => {
trackNoticeResetClick({'hasClickedNoticeReset': true})
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={handleResetClick}
sx={contentFlowSpacing}
>
Back to start
Expand Down
30 changes: 29 additions & 1 deletion editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ 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;

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;
Expand Down Expand Up @@ -104,6 +107,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
createAnalytics,
trackHelpClick,
trackNextStepsLinkClick,
trackNoticeResetClick,
node,
}}
>
Expand Down Expand Up @@ -179,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 = {}
) {
Expand All @@ -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 UpdateHasClickedNoticeReset(
$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({
Expand Down
Loading