diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx index c0338e85ad..440019ef43 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx @@ -155,12 +155,15 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({ node?.type === TYPES.Content ? getContentTitle(node) : node?.data?.title ?? node?.data?.text ?? node?.data?.flagSet; + const nodeId = node?.id || null; + // On component transition create the new analytics log const result = await insertNewAnalyticsLog( direction, analyticsId, metadata, nodeTitle, + nodeId, ); const id = result?.data.insert_analytics_logs_one?.id; const newLogCreatedAt = result?.data.insert_analytics_logs_one?.created_at; @@ -179,6 +182,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({ analyticsId: number, metadata: NodeMetadata, nodeTitle: string, + nodeId: string | null, ) { const result = await publicClient.mutate({ mutation: gql` @@ -188,7 +192,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({ $metadata: jsonb $node_type: Int $node_title: String - $user_agent: jsonb + $node_id: String ) { insert_analytics_logs_one( object: { @@ -198,6 +202,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({ metadata: $metadata node_type: $node_type node_title: $node_title + node_id: $node_id } ) { id @@ -211,6 +216,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({ metadata: metadata, node_type: node?.type, node_title: nodeTitle, + node_id: nodeId, }, }); return result; diff --git a/hasura.planx.uk/metadata/tables.yaml b/hasura.planx.uk/metadata/tables.yaml index fd28198a87..b1c03286cc 100644 --- a/hasura.planx.uk/metadata/tables.yaml +++ b/hasura.planx.uk/metadata/tables.yaml @@ -40,6 +40,7 @@ - id - input_errors - metadata + - node_id - node_title - node_type - user_exit diff --git a/hasura.planx.uk/migrations/1702897470388_alter_table_public_analytics_logs_add_column_node_id/down.sql b/hasura.planx.uk/migrations/1702897470388_alter_table_public_analytics_logs_add_column_node_id/down.sql new file mode 100644 index 0000000000..28812e635d --- /dev/null +++ b/hasura.planx.uk/migrations/1702897470388_alter_table_public_analytics_logs_add_column_node_id/down.sql @@ -0,0 +1 @@ +alter table "public"."analytics_logs" drop column "node_id"; diff --git a/hasura.planx.uk/migrations/1702897470388_alter_table_public_analytics_logs_add_column_node_id/up.sql b/hasura.planx.uk/migrations/1702897470388_alter_table_public_analytics_logs_add_column_node_id/up.sql new file mode 100644 index 0000000000..d245bf549b --- /dev/null +++ b/hasura.planx.uk/migrations/1702897470388_alter_table_public_analytics_logs_add_column_node_id/up.sql @@ -0,0 +1 @@ +alter table "public"."analytics_logs" add column "node_id" text null; diff --git a/hasura.planx.uk/migrations/1702899813258_update_view_analytics_summary_node_id/down.sql b/hasura.planx.uk/migrations/1702899813258_update_view_analytics_summary_node_id/down.sql new file mode 100644 index 0000000000..d46a60051c --- /dev/null +++ b/hasura.planx.uk/migrations/1702899813258_update_view_analytics_summary_node_id/down.sql @@ -0,0 +1,24 @@ +CREATE OR REPLACE VIEW public.analytics_summary AS +select + a.id as analytics_id, + al.id as analytics_log_id, + f.slug as service_slug, + t.slug as team_slug, + a.type as analytics_type, + a.created_at as analytics_created_at, + user_agent, + referrer, + flow_direction, + metadata, + al.user_exit as is_user_exit, + node_type, + node_title, + has_clicked_help, + input_errors, + CAST(EXTRACT(EPOCH FROM (al.next_log_created_at - al.created_at)) as numeric (10, 1)) as time_spent_on_node_seconds, + a.ended_at as analytics_ended_at, + CAST(EXTRACT(EPOCH FROM (a.ended_at - a.created_at))/60 as numeric (10, 1)) as time_spent_on_analytics_session_minutes +from analytics a + left join analytics_logs al on a.id = al.analytics_id + left join flows f on a.flow_id = f.id + left join teams t on t.id = f.team_id; diff --git a/hasura.planx.uk/migrations/1702899813258_update_view_analytics_summary_node_id/up.sql b/hasura.planx.uk/migrations/1702899813258_update_view_analytics_summary_node_id/up.sql new file mode 100644 index 0000000000..aab16af524 --- /dev/null +++ b/hasura.planx.uk/migrations/1702899813258_update_view_analytics_summary_node_id/up.sql @@ -0,0 +1,25 @@ +CREATE OR REPLACE VIEW public.analytics_summary AS +select + a.id as analytics_id, + al.id as analytics_log_id, + f.slug as service_slug, + t.slug as team_slug, + a.type as analytics_type, + a.created_at as analytics_created_at, + user_agent, + referrer, + flow_direction, + metadata, + al.user_exit as is_user_exit, + node_type, + node_title, + has_clicked_help, + input_errors, + CAST(EXTRACT(EPOCH FROM (al.next_log_created_at - al.created_at)) as numeric (10, 1)) as time_spent_on_node_seconds, + a.ended_at as analytics_ended_at, + CAST(EXTRACT(EPOCH FROM (a.ended_at - a.created_at))/60 as numeric (10, 1)) as time_spent_on_analytics_session_minutes, + node_id +from analytics a + left join analytics_logs al on a.id = al.analytics_id + left join flows f on a.flow_id = f.id + left join teams t on t.id = f.team_id;