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

- 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 committed Oct 17, 2023
1 parent 6d8e0fe commit d174255
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
70 changes: 66 additions & 4 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 @@ -10,7 +14,16 @@ export type AnalyticsType = "init" | "resume";
type AnalyticsLogDirection = AnalyticsType | "forwards" | "backwards";

export type HelpClickMetadata = Record<string, string>;
export type SelectedUrlsMetadata = Record<'selectedUrls', string[]>;
export type SelectedUrlsMetadata = Record<"selectedUrls", string[]>;

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

let lastAnalyticsLogId: number | undefined = undefined;

Expand Down Expand Up @@ -117,7 +130,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 @@ -138,6 +175,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}
) {
id
created_at
}
}
`,
Expand All @@ -149,8 +187,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 @@ -46,6 +47,7 @@
columns:
- 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 d174255

Please sign in to comment.