Skip to content

Commit 9bacbb4

Browse files
committed
Commit progress
1 parent 8c10239 commit 9bacbb4

File tree

7 files changed

+62
-68
lines changed

7 files changed

+62
-68
lines changed

demo/src/payload.config.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ import Tags from "./collections/Tags";
66
import Users from "./collections/Users";
77
import Media from "./collections/Media";
88
import payloadDashboardAnalytics from "../../src/index";
9+
import { PlausibleProvider } from "../../src/types/providers";
910

1011
const PLAUSIBLE_API_KEY = process.env.PLAUSIBLE_API_KEY;
1112
const PLAUSIBLE_HOST = process.env.PLAUSIBLE_HOST;
1213
const PLAUSIBLE_SITE_ID = process.env.PLAUSIBLE_SITE_ID;
1314

15+
const plausibleProvider: PlausibleProvider = {
16+
source: "plausible",
17+
apiSecret: PLAUSIBLE_API_KEY,
18+
siteId: PLAUSIBLE_SITE_ID,
19+
host: PLAUSIBLE_HOST,
20+
};
21+
1422
export default buildConfig({
1523
serverURL: "http://localhost:3000",
1624
admin: {
@@ -42,41 +50,40 @@ export default buildConfig({
4250
},
4351
plugins: [
4452
payloadDashboardAnalytics({
45-
provider: {
46-
source: "plausible",
47-
apiSecret: PLAUSIBLE_API_KEY,
48-
siteId: PLAUSIBLE_SITE_ID,
49-
host: PLAUSIBLE_HOST,
50-
},
53+
provider: plausibleProvider,
5154
collections: [
5255
{
5356
slug: Posts.slug,
5457
widgets: [
5558
{
5659
type: "chart",
57-
metrics: ["pageViews", "uniqueVisitors"],
60+
label: "Views and visitors",
61+
metrics: ["views", "visitors", "sessions"],
5862
timeframe: "30d",
5963
idMatcher: (document: any) => `/articles/${document.slug}`,
6064
},
6165
{
6266
type: "chart",
63-
metrics: ["pageViews"],
67+
metrics: ["sessions"],
6468
timeframe: "7d",
6569
idMatcher: (document: any) => `/articles/${document.slug}`,
6670
},
6771
{
6872
type: "chart",
69-
metrics: ["uniqueVisitors"],
73+
metrics: ["sessionDuration"],
7074
timeframe: "currentMonth",
7175
idMatcher: (document: any) => `/articles/${document.slug}`,
7276
},
7377
/* {
7478
type: "info",
75-
metric: "totalViews",
79+
metrics: ["views"],
80+
idMatcher: (document: any) => `/articles/${document.slug}`,
7681
},
7782
{
7883
type: "info",
79-
metric: "liveVisitors",
84+
metrics: ["sessions", "sessionDuration"],
85+
timeframe: "6mo",
86+
idMatcher: (document: any) => `/articles/${document.slug}`,
8087
}, */
8188
],
8289
},

src/components/Charts/AggregateDataWidget.tsx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,16 @@ import type { ChartDataPoint, ChartData } from "../../types/data";
1010
import type { PageInfoWidget } from "../../types/widgets";
1111
import type { AxisOptions } from "react-charts";
1212
import { useDocumentInfo } from "payload/components/utilities";
13-
import { MetricMap } from "../../providers/plausible/client";
13+
import { MetricMap } from "../../providers/plausible/utilities";
1414
import { useTheme } from "payload/dist/admin/components/utilities/Theme";
1515

16-
/* type ChartOptions = {
17-
timeframe?: string;
18-
metrics: ChartWidget["metrics"];
19-
idMatcher: IdMatcherFunction;
20-
}; */
21-
2216
type Props = {
2317
options: PageInfoWidget;
2418
};
2519

26-
const ChartComponent = lazy(() =>
27-
import("react-charts").then((module) => {
28-
return { default: module.Chart };
29-
})
30-
);
31-
3220
const AggregateDataWidget: React.FC<Props> = ({ options }) => {
3321
const [chartData, setChartData] = useState<ChartData>([]);
3422
const [isLoading, setIsLoading] = useState<boolean>(true);
35-
const chartRef = useRef<any>(null);
3623
const theme = useTheme();
3724
const { publishedDoc } = useDocumentInfo();
3825

@@ -65,12 +52,6 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
6552
}).then((response) => response.json());
6653

6754
getChartData.then((data: ChartData) => {
68-
/* const processedData: ChartData = [
69-
{
70-
label: "test",
71-
data: data,
72-
},
73-
]; */
7455
setChartData(data);
7556
setIsLoading(false);
7657
});

src/components/Charts/PageViewsChart.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import React, {
77
useMemo,
88
} from "react";
99
import type { ChartDataPoint, ChartData } from "../../types/data";
10-
import type { PageChartWidget } from "../../types/widgets";
10+
import type { PageChartWidget, Metrics } from "../../types/widgets";
1111
import type { AxisOptions } from "react-charts";
1212
import { useDocumentInfo } from "payload/components/utilities";
13-
import { MetricMap } from "../../providers/plausible/client";
13+
import { MetricMap } from "../../providers/plausible/utilities";
1414
import { useTheme } from "payload/dist/admin/components/utilities/Theme";
1515

1616
type Props = {
@@ -67,17 +67,21 @@ const PageViewsChart: React.FC<Props> = ({ options }) => {
6767
}, [publishedDoc, pageId]);
6868

6969
const chartLabel = useMemo(() => {
70-
if (label) return label;
70+
if (!!label) return label;
7171

72-
const metricValues: string[] = [];
72+
if (metrics) {
73+
const metricValues: string[] = [];
7374

74-
Object.entries(MetricMap).forEach(([key, value]) => {
75-
/* @ts-ignore */
76-
if (metrics.includes(key)) metricValues.push(value.label);
77-
});
75+
Object.entries(MetricMap).forEach(([key, value]) => {
76+
// @ts-ignore
77+
if (metrics.includes(key)) metricValues.push(value.label);
78+
});
7879

79-
return metricValues.join(", ");
80-
}, [options]);
80+
return metricValues.join(", ");
81+
} else {
82+
return "No metrics defined for this widget";
83+
}
84+
}, [label, metrics]);
8185

8286
const primaryAxis = React.useMemo<AxisOptions<ChartDataPoint>>(() => {
8387
return {

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import getProvider from "./providers";
1111
import getGlobalAggregateData from "./routes/getGlobalAggregateData";
1212
import getGlobalChartData from "./routes/getGlobalChartData";
1313
import getPageChartData from "./routes/getPageChartData";
14+
import getPageAggregateData from "./routes/getPageAggregateData";
1415
import type { CollectionConfig } from "payload/dist/collections/config/types";
1516
import { getPageViewsChart } from "./components/Charts/PageViewsChart";
1617
import { getAggregateDataWidget } from "./components/Charts/AggregateDataWidget";
@@ -63,6 +64,7 @@ const payloadDashboardAnalytics =
6364
getGlobalAggregateData(apiProvider),
6465
getGlobalChartData(apiProvider),
6566
getPageChartData(apiProvider),
67+
getPageAggregateData(apiProvider),
6668
],
6769
...(collections && {
6870
collections: collections.map((collection) => {

src/providers/plausible/client.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
import type { PlausibleProvider } from "../../types/providers";
22
import type { AllAvailableMetrics } from "../../types/widgets";
3+
import { MetricMap } from "./utilities";
34

45
type ClientOptions = {
56
endpoint: string;
67
timeframe?: string;
78
metrics?: AllAvailableMetrics[];
89
};
910

10-
export const MetricMap: Record<
11-
AllAvailableMetrics,
12-
{ label: string; value: string }
13-
> = {
14-
pageViews: {
15-
label: "Page views",
16-
value: "pageviews",
17-
},
18-
uniqueVisitors: { label: "Visitors", value: "visitors" },
19-
bounceRate: { label: "Bounce rate", value: "visitors" },
20-
averageDuration: { label: "Avg. duration", value: "visitors" },
21-
totalViews: { label: "Total views", value: "visitors" },
22-
totalVisitors: { label: "Total visitors", value: "visitors" },
23-
};
24-
2511
function client(provider: PlausibleProvider, options: ClientOptions) {
2612
const { endpoint, timeframe, metrics } = options;
2713
const host = provider.host ?? `https://plausible.io`;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { AllAvailableMetrics } from "../../types/widgets";
2+
3+
export const MetricMap: Record<
4+
AllAvailableMetrics,
5+
{ label: string; value: string }
6+
> = {
7+
views: {
8+
label: "Views",
9+
value: "pageviews",
10+
},
11+
visitors: { label: "Visitors", value: "visitors" },
12+
bounceRate: { label: "Bounce rate", value: "bounce_rate" },
13+
sessionDuration: { label: "Avg. duration", value: "visit_duration" },
14+
sessions: { label: "Sessions", value: "visits" },
15+
};

src/types/widgets.d.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@ export type Timeframes = "12mo" | "6mo" | "30d" | "7d" | "currentMonth";
22

33
export type IdMatcherFunction = (document: any) => string;
44

5-
export type ChartWidgetMetrics = "pageViews" | "uniqueVisitors";
5+
export type Metrics =
6+
| "views"
7+
| "visitors"
8+
| "sessions"
9+
| "bounceRate"
10+
| "sessionDuration";
611

712
export interface ChartWidget {
813
type: "chart";
914
label?: string;
10-
metrics: ChartWidgetMetrics[];
11-
timeframe: Timeframes;
15+
metrics: Metrics[];
16+
timeframe?: Timeframes;
1217
}
1318

1419
export interface PageChartWidget extends ChartWidget {
1520
idMatcher: IdMatcherFunction;
1621
}
1722

18-
export type InfoWidgetMetrics =
19-
| "totalViews"
20-
| "totalVisitors"
21-
| "bounceRate"
22-
| "averageDuration";
23-
24-
export type AllAvailableMetrics = ChartWidgetMetrics | InfoWidgetMetrics;
23+
export type AllAvailableMetrics = Metrics;
2524

2625
export interface InfoWidget {
2726
type: "info";
2827
label?: string;
29-
metrics: InfoWidgetMetrics[];
30-
timeframe: Timeframes;
28+
metrics: Metrics[];
29+
timeframe?: Timeframes;
3130
}
3231

3332
export interface PageInfoWidget extends InfoWidget {

0 commit comments

Comments
 (0)