Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Capture user agent and referrer analytics #2349

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions editor.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@turf/helpers": "^6.5.0",
"array-move": "^4.0.0",
"axios": "0.27.2",
"bowser": "^2.11.0",
"camelcase-keys": "^9.0.0",
"classnames": "^2.3.2",
"core-js": "^3.31.0",
Expand Down
7 changes: 7 additions & 0 deletions editor.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FlagSet,
} from "@opensystemslab/planx-core/types";
import { TYPES } from "@planx/components/types";
import Bowser from "bowser";
import { publicClient } from "lib/graphql";
import React, { createContext, useContext, useEffect, useState } from "react";

Expand Down Expand Up @@ -129,7 +130,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({

async function track(direction: AnalyticsLogDirection, analyticsId: number) {
const metadata = getNodeMetadata();
const node_title =
const nodeTitle =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great spot and tidy up 🙌

node?.type === TYPES.Content
? getContentTitle(node)
: node?.data?.title ?? node?.data?.text ?? node?.data?.flagSet;
Expand All @@ -138,7 +139,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
direction,
analyticsId,
metadata,
node_title,
nodeTitle,
);
const id = result?.data.insert_analytics_logs_one?.id;
const newLogCreatedAt = result?.data.insert_analytics_logs_one?.created_at;
Expand All @@ -156,7 +157,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
direction: AnalyticsLogDirection,
analyticsId: number,
metadata: NodeMetadata,
node_title: string,
nodeTitle: string,
) {
const result = await publicClient.mutate({
mutation: gql`
Expand All @@ -166,6 +167,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
$metadata: jsonb
$node_type: Int
$node_title: String
$user_agent: jsonb
) {
insert_analytics_logs_one(
object: {
Expand All @@ -187,7 +189,7 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
analytics_id: analyticsId,
metadata: metadata,
node_type: node?.type,
node_title: node_title,
node_title: nodeTitle,
},
});
return result;
Expand Down Expand Up @@ -287,17 +289,34 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({

async function createAnalytics(type: AnalyticsType) {
if (shouldTrackAnalytics) {
const userAgent = Bowser.parse(window.navigator.userAgent);
const referrer = document.referrer || null;

const response = await publicClient.mutate({
mutation: gql`
mutation InsertNewAnalytics($type: String, $flow_id: uuid) {
insert_analytics_one(object: { type: $type, flow_id: $flow_id }) {
mutation InsertNewAnalytics(
$type: String
$flow_id: uuid
$user_agent: jsonb
$referrer: String
) {
insert_analytics_one(
object: {
type: $type
flow_id: $flow_id
user_agent: $user_agent
referrer: $referrer
}
) {
id
}
}
`,
variables: {
type,
flow_id: flowId,
user_agent: userAgent,
referrer,
},
});
const id = response.data.insert_analytics_one.id;
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 @@ -9,7 +9,9 @@
- created_at
- flow_id
- id
- referrer
- type
- user_agent
select_permissions:
- role: public
permission:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "public"."analytics" DROP COLUMN "referrer"

ALTER TABLE "public"."analytics" DROP COLUMN "user_agent"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "public"."analytics" add column "user_agent" jsonb
null default '{}';

alter table "public"."analytics" add column "referrer" text
null;
Loading