Skip to content

Commit

Permalink
refactor: update metadata type definitions to more explicitly describ…
Browse files Browse the repository at this point in the history
…e metadata column
  • Loading branch information
Mike-Heneghan committed Feb 21, 2024
1 parent e0f5114 commit 8df4c1e
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,41 @@ const ALLOW_LIST = [
] as const;

export type HelpClickMetadata = Record<string, string>;
export type SelectedUrlsMetadata = Record<"selectedUrls", string[]>;
export type BackwardsNavigationInitiatorType = "change" | "back";
type SelectedUrlsMetadata = Record<"selectedUrls", string[]>;
type BackwardsNavigationInitiatorType = "change" | "back";

type NodeMetadata = {
type ResultNodeMetadata = {
flag?: Flag;
flagSet?: FlagSet;
displayText?: {
heading?: string;
description?: string;
};
flag?: Flag;
title?: string;
type?: string | null;
id?: string;
};

type AutoAnswerMetadata = {
isAutoAnswered?: boolean;
};

type NodeMetadata = ResultNodeMetadata & AutoAnswerMetadata;

type BackwardsTargetMetadata = {
id?: string;
type?: string | null;
title?: string;
};

type BackwardsNavigationMetadata =
| Record<"change", BackwardsTargetMetadata>
| Record<"back", BackwardsTargetMetadata>;

/**
* Describes the value held in analytics_logs.metadata
* Describes the possible values that can be written to analytics_logs.metadata
* by any one of the specific tracking event functions
*/
type Metadata =
| Record<"change", NodeMetadata>
| Record<"back", NodeMetadata>
| NodeMetadata
| BackwardsNavigationMetadata
| SelectedUrlsMetadata
| HelpClickMetadata;

Expand Down Expand Up @@ -377,7 +390,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
if (shouldSkipTracking()) return;

const targetNodeMetadata = nodeId ? getTargetNodeDataFromFlow(nodeId) : {};
const metadata: Metadata =
const metadata: BackwardsNavigationMetadata =
initiator === "change"
? { change: targetNodeMetadata }
: { back: targetNodeMetadata };
Expand Down Expand Up @@ -509,7 +522,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
function getTargetNodeDataFromFlow(nodeId: string) {
const node = flow[nodeId];
const nodeType = node?.type ? TYPES[node.type] : null;
const nodeMetadata: NodeMetadata = {
const nodeMetadata: BackwardsTargetMetadata = {
title: extractNodeTitle(node),
type: nodeType,
id: nodeId,
Expand Down

0 comments on commit 8df4c1e

Please sign in to comment.