-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
166 lines (151 loc) · 4.32 KB
/
index.ts
File metadata and controls
166 lines (151 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type { AdminViewConfig, Config, Plugin } from 'payload'
import { analyticsDataEndpoint } from './endpoints/data'
// Re-export all types
export type {
AnalyticsPluginOptions,
DashboardConfig,
CardConfig,
ChartConfig,
TableConfig,
} from './types'
export type {
TimePeriodData,
PageData,
SourceData,
EventData,
StatsData,
PostHogData,
TimePeriod,
} from './lib/posthog.types'
// Re-export reusable components for custom dashboards
export { AnalyticsCard } from './components/AnalyticsCard'
export { Table } from './components/Table'
export { ChartRenderer } from './components/ChartRenderer'
export { useAnalytics } from './lib/use-analytics'
export { formatNumber } from './lib/utils'
import type { AnalyticsPluginOptions } from './types'
// Module-level storage for dashboard config
let dashboardConfig: AnalyticsPluginOptions['dashboard'] | undefined
/**
* Get the current dashboard configuration
* This is used by the AnalyticsDashboard component
*/
export const getDashboardConfig = () => dashboardConfig
/**
* PostHog Analytics Plugin for Payload CMS
*
* Adds an analytics dashboard to your Payload admin panel with PostHog integration.
*
* @example
* ```ts
* import { analyticsPlugin } from 'payload-posthog-analytics'
*
* export default buildConfig({
* plugins: [
* analyticsPlugin({
* adminView: {
* path: '/analytics',
* label: 'Analytics',
* },
* dashboard: {
* cards: [
* { key: 'visitors', title: 'Unique Visitors' },
* { key: 'pageViews', title: 'Page Views' },
* ],
* charts: [
* {
* id: 'visitors-chart',
* title: 'Visitor Trends',
* type: 'area',
* dataKey: 'visitors',
* color: '#10b981',
* },
* ],
* },
* }),
* ],
* })
* ```
*/
export const analyticsPlugin = (
pluginOptions: AnalyticsPluginOptions = {},
): Plugin => {
return (incomingConfig: Config): Config => {
const options: Required<Omit<AnalyticsPluginOptions, 'dashboard'>> & Pick<AnalyticsPluginOptions, 'dashboard'> = {
enabled: pluginOptions.enabled ?? true,
adminView: {
path: pluginOptions.adminView?.path ?? '/analytics',
label: pluginOptions.adminView?.label ?? 'Analytics',
requireAuth: pluginOptions.adminView?.requireAuth ?? true,
},
reverseProxy: {
enabled: pluginOptions.reverseProxy?.enabled ?? true,
ingestPath: pluginOptions.reverseProxy?.ingestPath ?? '/ingest',
},
dashboard: pluginOptions.dashboard,
}
if (!options.enabled) {
return incomingConfig
}
// Store dashboard config for access by client components
dashboardConfig = options.dashboard
const config = { ...incomingConfig }
config.admin = {
...config.admin,
components: {
...config.admin?.components,
views: {
...config.admin?.components?.views,
[(options.adminView.label || "").toLowerCase()]: {
Component: 'payload-posthog-analytics/components/AnalyticsView#AnalyticsView',
path: options.adminView.path,
} as AdminViewConfig,
},
},
}
config.endpoints = [
...(config.endpoints || []),
analyticsDataEndpoint,
]
return config
}
}
/**
* Helper function to generate Next.js rewrites for PostHog reverse proxy
* This helps bypass ad blockers by proxying PostHog requests through your domain.
*
* Add this to your next.config.js:
*
* @example
* ```js
* import { getAnalyticsRewrites } from './src/plugins/analytics'
*
* const nextConfig = {
* async rewrites() {
* return [
* ...getAnalyticsRewrites(),
* // your other rewrites
* ]
* },
* }
* ```
*/
export const getAnalyticsRewrites = (options?: {
ingestPath?: string
posthogHost?: string
posthogAssetsHost?: string
}) => {
const ingestPath = options?.ingestPath || '/ingest'
const posthogHost = options?.posthogHost || 'https://us.i.posthog.com'
const posthogAssetsHost = options?.posthogAssetsHost || 'https://us-assets.i.posthog.com'
return [
{
source: `${ingestPath}/static/:path*`,
destination: `${posthogAssetsHost}/static/:path*`,
},
{
source: `${ingestPath}/:path*`,
destination: `${posthogHost}/:path*`,
},
]
}