Skip to content

Commit 26bac48

Browse files
committed
Commit progress on adding routes for global data
1 parent 12dc017 commit 26bac48

File tree

11 files changed

+381
-254
lines changed

11 files changed

+381
-254
lines changed

demo/src/payload.config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import Users from "./collections/Users";
77
import Media from "./collections/Media";
88
import payloadDashboardAnalytics from "../../src/index";
99

10+
const PLAUSIBLE_API_KEY = process.env.PLAUSIBLE_API_KEY;
11+
const PLAUSIBLE_HOST = process.env.PLAUSIBLE_HOST;
12+
const PLAUSIBLE_SITE_ID = process.env.PLAUSIBLE_SITE_ID;
13+
1014
export default buildConfig({
1115
serverURL: "http://localhost:3000",
1216
admin: {
@@ -23,8 +27,9 @@ export default buildConfig({
2327
payloadDashboardAnalytics({
2428
provider: {
2529
source: "plausible",
26-
apiSecret: "placeholder",
27-
websiteId: "payloadDemo",
30+
apiSecret: PLAUSIBLE_API_KEY,
31+
siteId: PLAUSIBLE_SITE_ID,
32+
host: PLAUSIBLE_HOST,
2833
},
2934
}),
3035
],

demo/yarn.lock

Lines changed: 123 additions & 123 deletions
Large diffs are not rendered by default.

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { Config as PayloadConfig } from "payload/config";
22
import { DashboardAnalyticsConfig } from "./types";
33
import { extendWebpackConfig } from "./extendWebpackConfig";
4+
import getProvider from "./providers";
5+
import getGlobalAggregateData from "./routes/getGlobalAggregateData";
46

57
const payloadDashboardAnalytics =
68
(incomingConfig: DashboardAnalyticsConfig) =>
79
(config: PayloadConfig): PayloadConfig => {
810
const { admin } = config;
11+
const { provider } = incomingConfig;
12+
const endpoints = config.endpoints ?? [];
13+
const apiProvider = getProvider(provider);
914

1015
const processedConfig: PayloadConfig = {
16+
...config,
1117
admin: {
1218
...admin,
1319
webpack: extendWebpackConfig(config),
1420
},
21+
endpoints: [...endpoints, getGlobalAggregateData(apiProvider)],
1522
};
1623

1724
return processedConfig;

src/providers/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import plausible from "./plausible";
2+
import type { Provider } from "../types";
3+
4+
export type ApiProvider = {
5+
getGlobalAggregateData: () => Promise<any>;
6+
/* getGlobalChartData: () => {},
7+
getPageAggregateData: () => {},
8+
getPageChartData: () => {}, */
9+
};
10+
11+
const getProvider = (provider: Provider) => {
12+
switch (provider.source) {
13+
case "plausible":
14+
return plausible(provider);
15+
}
16+
};
17+
18+
export default getProvider;

src/providers/plausible/client.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { PlausibleProvider } from "../../types";
2+
3+
function client(provider: PlausibleProvider, endpoint?: string) {
4+
const host = provider.host ?? `https://plausible.io`;
5+
const apiVersion = `v1`; // for future use
6+
7+
const url = new URL(`${host}/api/${apiVersion}${endpoint}`);
8+
url.searchParams.append("site_id", provider.siteId);
9+
10+
const plausibleMetrics = [
11+
"visitors",
12+
"pageviews",
13+
"bounce_rate",
14+
"visit_duration",
15+
];
16+
17+
const baseUrl = String(url.href);
18+
url.searchParams.append("period", "30d");
19+
url.searchParams.append("metrics", String(plausibleMetrics));
20+
21+
return {
22+
host: host,
23+
baseUrl: baseUrl,
24+
url: url,
25+
fetch: async () =>
26+
await fetch(url, {
27+
method: "get",
28+
headers: new Headers({
29+
Authorization: `Bearer ${provider.apiSecret}`,
30+
"Content-Type": "application/x-www-form-urlencoded",
31+
}),
32+
}),
33+
};
34+
}
35+
36+
export default client;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { PlausibleProvider } from "../../types";
2+
import client from "./client";
3+
4+
async function getGlobalAggregateData(provider: PlausibleProvider) {
5+
const plausibleClient = client(provider, "/stats/aggregate");
6+
7+
const data = await plausibleClient.fetch().then((response) => {
8+
return response.json();
9+
});
10+
11+
return data;
12+
}
13+
14+
export default getGlobalAggregateData;

src/providers/plausible/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { PlausibleProvider } from "../../types";
2+
import getGlobalAggregateData from "./getGlobalAggregateData";
3+
import { ApiProvider } from "..";
4+
5+
const plausible = (provider: PlausibleProvider): ApiProvider => {
6+
return {
7+
getGlobalAggregateData: async () => await getGlobalAggregateData(provider),
8+
/* getGlobalChartData: () => {},
9+
getPageAggregateData: () => {},
10+
getPageChartData: () => {}, */
11+
};
12+
};
13+
14+
export default plausible;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Endpoint } from "payload/config";
2+
import { ApiProvider } from "../../providers";
3+
import payload from "payload";
4+
5+
const handler = (provider: ApiProvider) => {
6+
const handler: Endpoint["handler"] = async (req, res, next) => {
7+
try {
8+
const data = await provider.getGlobalAggregateData();
9+
res.status(200).send(data);
10+
} catch (error) {
11+
payload.logger.error(payload);
12+
res.status(500);
13+
}
14+
};
15+
16+
return handler;
17+
};
18+
19+
export default handler;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Endpoint } from "payload/config";
2+
import handler from "./handler";
3+
import { ApiProvider } from "../../providers";
4+
5+
const getGlobalAggregateData = (provider: ApiProvider): Endpoint => {
6+
return {
7+
path: "/analytics/globalAggregateData",
8+
method: "get",
9+
handler: handler(provider),
10+
};
11+
};
12+
13+
export default getGlobalAggregateData;

src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Payload } from "payload";
22
import { Config as PayloadConfig } from "payload/config";
33

4-
interface PlausibleProvider {
4+
export interface PlausibleProvider {
55
source: "plausible";
66
apiSecret: string;
7-
websiteId: string;
7+
siteId: string;
8+
host?: string;
89
}
910

1011
interface GoogleProvider {
@@ -13,7 +14,7 @@ interface GoogleProvider {
1314
propertyId: string;
1415
}
1516

16-
type Provider = PlausibleProvider | GoogleProvider;
17+
export type Provider = PlausibleProvider;
1718

1819
export type DashboardAnalyticsConfig = {
1920
provider: Provider;

0 commit comments

Comments
 (0)