Skip to content

Commit

Permalink
feat: update analytics_log with the created_at of new analytics_log r…
Browse files Browse the repository at this point in the history
…ecord on next_log_created_at (#2314)

- Add next_log_created_at column to analytics_log
- Update permissions to allow created_at to be read via graphql select
- On component transition when an analytics log is created store the created at
- Update the last log with this value
- Should allow us to infer the time spent per card in a single session
  • Loading branch information
Mike-Heneghan authored Oct 18, 2023
1 parent 9515b88 commit d86cd87
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion editor.planx.uk/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ const PublicToolbar: React.FC<{
const showCentredServiceTitle = useMediaQuery((theme: Theme) =>
theme.breakpoints.up("md"),
);

const { trackResetFlow } = useAnalyticsTracking();

const handleRestart = async () => {
Expand Down
68 changes: 65 additions & 3 deletions editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { gql } from "@apollo/client";
import { DEFAULT_FLAG_CATEGORY } from "@opensystemslab/planx-core/types";
import {
DEFAULT_FLAG_CATEGORY,
Flag,
FlagSet,
} from "@opensystemslab/planx-core/types";
import { TYPES } from "@planx/components/types";
import { publicClient } from "lib/graphql";
import React, { createContext, useContext, useEffect, useState } from "react";
Expand All @@ -12,6 +16,15 @@ type AnalyticsLogDirection = AnalyticsType | "forwards" | "backwards" | "reset";
export type HelpClickMetadata = Record<string, string>;
export type SelectedUrlsMetadata = Record<"selectedUrls", string[]>;

type NodeMetadata = {
flagset?: FlagSet;
displayText?: {
heading?: string;
description?: string;
};
flag?: Flag;
};

let lastAnalyticsLogId: number | undefined = undefined;

const analyticsContext = createContext<{
Expand Down Expand Up @@ -120,7 +133,31 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
node?.type === TYPES.Content
? getContentTitle(node)
: node?.data?.title ?? node?.data?.text ?? node?.data?.flagSet;
// On component transition create the new analytics log
const result = await insertNewAnalyticsLog(
direction,
analyticsId,
metadata,
node_title,
);
const id = result?.data.insert_analytics_logs_one?.id;
const newLogCreatedAt = result?.data.insert_analytics_logs_one?.created_at;

// On successful create of a new log update the previous log with the next_log_created_at
// This allows us to estimate how long a user spend on a card
if (lastAnalyticsLogId && newLogCreatedAt) {
updateLastLogWithNextLogCreatedAt(lastAnalyticsLogId, newLogCreatedAt);
}

lastAnalyticsLogId = id;
}

async function insertNewAnalyticsLog(
direction: AnalyticsLogDirection,
analyticsId: number,
metadata: NodeMetadata,
node_title: string,
) {
const result = await publicClient.mutate({
mutation: gql`
mutation InsertNewAnalyticsLog(
Expand All @@ -141,6 +178,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}
) {
id
created_at
}
}
`,
Expand All @@ -152,8 +190,32 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
node_title: node_title,
},
});
const id = result?.data.insert_analytics_logs_one?.id;
lastAnalyticsLogId = id;
return result;
}

async function updateLastLogWithNextLogCreatedAt(
lastAnalyticsLogId: number,
newLogCreatedAt: Date,
) {
await publicClient.mutate({
mutation: gql`
mutation UpdateNextLogCreatedAt(
$id: bigint!
$next_log_created_at: timestamptz
) {
update_analytics_logs_by_pk(
pk_columns: { id: $id }
_set: { next_log_created_at: $next_log_created_at }
) {
id
}
}
`,
variables: {
id: lastAnalyticsLogId,
next_log_created_at: newLogCreatedAt,
},
});
}

async function trackHelpClick(metadata?: HelpClickMetadata) {
Expand Down
2 changes: 2 additions & 0 deletions hasura.planx.uk/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
permission:
columns:
- analytics_id
- created_at
- id
filter: {}
update_permissions:
Expand All @@ -47,6 +48,7 @@
- flow_direction
- has_clicked_help
- metadata
- next_log_created_at
filter: {}
check: null
- table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."analytics_logs" drop column "next_log_created_at";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."analytics_logs" add column "next_log_created_at" timestamp with time zone
null;

0 comments on commit d86cd87

Please sign in to comment.