Skip to content

Commit

Permalink
feat: track which url is navigated to from Next Steps component (#2282)
Browse files Browse the repository at this point in the history
* feat: add analytics_log for which url was selected on a next steps link click

- Store user selection of urls in analytics_log table metadata column under `selectedUrls`
  • Loading branch information
Mike-Heneghan authored Oct 10, 2023
1 parent 238b548 commit 4fc5b43
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
6 changes: 5 additions & 1 deletion editor.planx.uk/src/@planx/components/NextSteps/Public.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PublicProps } from "@planx/components/ui";

import React from "react";
import NextStepsList from "ui/NextStepsList";

Expand All @@ -18,7 +19,10 @@ const NextStepsComponent: React.FC<Props> = (props) => {
policyRef={props.policyRef}
howMeasured={props.howMeasured}
/>
<NextStepsList steps={props.steps} handleSubmit={props.handleSubmit} />
<NextStepsList
steps={props.steps}
handleSubmit={props.handleSubmit}
/>
</Card>
);
};
Expand Down
28 changes: 28 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ export type AnalyticsType = "init" | "resume";
type AnalyticsLogDirection = AnalyticsType | "forwards" | "backwards";

export type HelpClickMetadata = Record<string, 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>;
node: Store.node | null;
}>({
createAnalytics: () => Promise.resolve(),
trackHelpClick: () => Promise.resolve(),
trackNextStepsLinkClick: () => Promise.resolve(),
node: null,
});
const { Provider } = analyticsContext;
Expand Down Expand Up @@ -100,6 +103,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
value={{
createAnalytics,
trackHelpClick,
trackNextStepsLinkClick,
node,
}}
>
Expand Down Expand Up @@ -171,6 +175,30 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}
}

async function trackNextStepsLinkClick(metadata?: SelectedUrlsMetadata) {
if (shouldTrackAnalytics && lastAnalyticsLogId) {
await publicClient.mutate({
mutation: gql`
mutation UpdateHasClickNextStepsLink(
$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
31 changes: 24 additions & 7 deletions editor.planx.uk/src/ui/NextStepsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Link from "@mui/material/Link";
import { styled, Theme } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import type { Step as StyledListItem } from "@planx/components/NextSteps/model";
import { useAnalyticsTracking } from "pages/FlowEditor/lib/analyticsProvider";
import { handleSubmit } from "pages/Preview/Node";
import React from "react";
import React, { useState } from "react";

interface NextStepsListProps {
steps: StyledListItem[];
Expand All @@ -15,6 +16,7 @@ interface NextStepsListProps {

interface ListItemProps extends StyledListItem {
handleSubmit?: handleSubmit;
handleSelectingUrl?: (url: string) => void;
}

const Root = styled("ul")(({ theme }) => ({
Expand Down Expand Up @@ -72,11 +74,18 @@ const ArrowButton = styled("span")(({ theme }) => ({
flexShrink: "0",
}));

const LinkStep = (props: ListItemProps) => (
<InnerLink href={props.url} target="_blank" rel="noopener">
<Step {...props} />
</InnerLink>
);
function LinkStep(props: ListItemProps) {
return (
<InnerLink
href={props.url}
target="_blank"
rel="noopener"
onClick={() => props.handleSelectingUrl && props.url && props.handleSelectingUrl(props.url)}
>
<Step {...props} />
</InnerLink>
);
}

const ContinueStep = (props: ListItemProps) => (
<InnerButton onClick={() => props.handleSubmit && props.handleSubmit()}>
Expand Down Expand Up @@ -109,12 +118,20 @@ const Step = ({ title, description, url }: ListItemProps) => (
);

function NextStepsList(props: NextStepsListProps) {
const [selectedUrls, setSelectedUrls] = useState<string[]>([]);
const { trackNextStepsLinkClick } = useAnalyticsTracking()

const handleSelectingUrl = (newUrl: string) => {
setSelectedUrls(prevSelectedUrls => [...prevSelectedUrls, newUrl]);
trackNextStepsLinkClick({'selectedUrls': [...selectedUrls, newUrl]})
}

return (
<Root>
{props.steps?.map((step, i) => (
<StyledListItem key={i}>
{step.url ? (
<LinkStep {...step} />
<LinkStep {...step} handleSelectingUrl={handleSelectingUrl}/>
) : (
<ContinueStep {...step} handleSubmit={props.handleSubmit} />
)}
Expand Down

0 comments on commit 4fc5b43

Please sign in to comment.