Skip to content

Commit 8c10239

Browse files
committed
Update types for page aggregate data too
1 parent 55ab23f commit 8c10239

File tree

12 files changed

+77
-16
lines changed

12 files changed

+77
-16
lines changed

demo/src/payload.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default buildConfig({
6767
{
6868
type: "chart",
6969
metrics: ["uniqueVisitors"],
70-
timeframe: "month",
70+
timeframe: "currentMonth",
7171
idMatcher: (document: any) => `/articles/${document.slug}`,
7272
},
7373
/* {

src/extendWebpackConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export const extendWebpackConfig =
2121
? existingWebpackConfig.resolve.alias
2222
: {}),
2323
express: mockModulePath,
24+
[path.resolve(__dirname, "./providers/plausible/client")]:
25+
mockModulePath,
2426
},
2527
},
2628
};

src/providers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface GlobalChartOptions extends BaseOptions {
1515

1616
export interface PageAggregateOptions extends BaseOptions {
1717
metrics: InfoWidget["metrics"];
18+
pageId: string;
1819
}
1920
export interface PageChartOptions extends BaseOptions {
2021
metrics: ChartWidget["metrics"];
@@ -24,7 +25,7 @@ export interface PageChartOptions extends BaseOptions {
2425
export type ApiProvider = {
2526
getGlobalAggregateData: (options: GlobalAggregateOptions) => Promise<any>;
2627
getGlobalChartData: (options: GlobalChartOptions) => Promise<any>;
27-
/* getPageAggregateData: (options?: PageAggregateOptions) => Promise<any>; */
28+
getPageAggregateData: (options: PageAggregateOptions) => Promise<any>;
2829
getPageChartData: (options: PageChartOptions) => Promise<any>;
2930
};
3031

src/providers/plausible/client.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ function client(provider: PlausibleProvider, options: ClientOptions) {
2727
const host = provider.host ?? `https://plausible.io`;
2828
const apiVersion = `v1`; // for future use
2929

30-
const period = timeframe ?? "30d";
30+
const period = () => {
31+
switch (timeframe) {
32+
case "currentMonth":
33+
return "month";
34+
case null:
35+
case undefined:
36+
return "30d";
37+
default:
38+
return timeframe;
39+
}
40+
};
3141

3242
const url = new URL(`${host}/api/${apiVersion}${endpoint}`);
3343
url.searchParams.append("site_id", provider.siteId);
@@ -50,7 +60,7 @@ function client(provider: PlausibleProvider, options: ClientOptions) {
5060
const plausibleMetrics = metrics?.length ? getMetrics() : "pageviews";
5161

5262
const baseUrl = String(url.href);
53-
url.searchParams.append("period", period);
63+
url.searchParams.append("period", period());
5464
url.searchParams.append("metrics", String(plausibleMetrics));
5565

5666
return {
@@ -61,8 +71,6 @@ function client(provider: PlausibleProvider, options: ClientOptions) {
6171
fetch: async (customUrl?: string) => {
6272
const fetchUrl = customUrl ?? url.toString();
6373

64-
console.log("fetching data with ", fetchUrl);
65-
6674
return await fetch(fetchUrl, {
6775
method: "get",
6876
headers: new Headers({

src/providers/plausible/getGlobalAggregateData.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import client from "./client";
44

55
async function getGlobalAggregateData(
66
provider: PlausibleProvider,
7-
options?: GlobalAggregateOptions
7+
options: GlobalAggregateOptions
88
) {
99
const plausibleClient = client(provider, {
1010
endpoint: "/stats/aggregate",
11-
timeframe: options?.timeframe,
11+
timeframe: options.timeframe,
12+
metrics: options.metrics,
1213
});
1314

1415
const data = await plausibleClient.fetch().then((response) => {

src/providers/plausible/getGlobalChartData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function getGlobalChartData(
1212
const plausibleClient = client(provider, {
1313
endpoint: "/stats/timeseries",
1414
timeframe: options?.timeframe,
15-
metrics: options?.metrics,
15+
metrics: options.metrics,
1616
});
1717

1818
const { results } = await plausibleClient

src/providers/plausible/getPageAggregateData.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import type { PlausibleProvider } from "../../types/providers";
2-
import type { GlobalAggregateOptions } from "..";
2+
import type { PageAggregateOptions } from "..";
33
import client from "./client";
44

55
async function getPageAggregateData(
66
provider: PlausibleProvider,
7-
options?: GlobalAggregateOptions
7+
options: PageAggregateOptions
88
) {
99
const plausibleClient = client(provider, {
1010
endpoint: "/stats/aggregate",
1111
timeframe: options?.timeframe,
12+
metrics: options.metrics,
1213
});
1314

14-
const data = await plausibleClient.fetch().then((response) => {
15+
const url = plausibleClient.url;
16+
17+
const pageFilter = `event:page==${options.pageId}`;
18+
19+
url.searchParams.append("filters", pageFilter);
20+
21+
const data = await plausibleClient.fetch(url.toString()).then((response) => {
1522
return response.json();
1623
});
1724

src/providers/plausible/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const plausible = (provider: PlausibleProvider): ApiProvider => {
1919
await getGlobalChartData(provider, options),
2020
getPageChartData: async (options: PageChartOptions) =>
2121
await getPageChartData(provider, options),
22+
getPageAggregateData: async (options: PageAggregateOptions) =>
23+
await getPageAggregateData(provider, options),
2224
};
2325
};
2426

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 { timeframe, metrics, pageId } = req.body;
9+
10+
console.log("reached handler with", timeframe, metrics, pageId);
11+
12+
const data = await provider.getPageAggregateData({
13+
timeframe: timeframe,
14+
metrics: metrics,
15+
pageId,
16+
});
17+
res.status(200).send(data);
18+
} catch (error) {
19+
payload.logger.error(error);
20+
res.status(500);
21+
}
22+
};
23+
24+
return handler;
25+
};
26+
27+
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 getPageAggregateData = (provider: ApiProvider): Endpoint => {
6+
return {
7+
path: "/analytics/pageAggregateData",
8+
method: "post",
9+
handler: handler(provider),
10+
};
11+
};
12+
13+
export default getPageAggregateData;

0 commit comments

Comments
 (0)