File tree Expand file tree Collapse file tree 11 files changed +381
-254
lines changed
routes/getGlobalAggregateData Expand file tree Collapse file tree 11 files changed +381
-254
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ import Users from "./collections/Users";
77import Media from "./collections/Media" ;
88import 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+
1014export 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 ] ,
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 11import { Config as PayloadConfig } from "payload/config" ;
22import { DashboardAnalyticsConfig } from "./types" ;
33import { extendWebpackConfig } from "./extendWebpackConfig" ;
4+ import getProvider from "./providers" ;
5+ import getGlobalAggregateData from "./routes/getGlobalAggregateData" ;
46
57const 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 ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 11import { Payload } from "payload" ;
22import { 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
1011interface GoogleProvider {
@@ -13,7 +14,7 @@ interface GoogleProvider {
1314 propertyId : string ;
1415}
1516
16- type Provider = PlausibleProvider | GoogleProvider ;
17+ export type Provider = PlausibleProvider ;
1718
1819export type DashboardAnalyticsConfig = {
1920 provider : Provider ;
You can’t perform that action at this time.
0 commit comments