Skip to content

Commit

Permalink
Integrate PostHog analytics 🦔
Browse files Browse the repository at this point in the history
  • Loading branch information
wotschofsky committed Dec 18, 2024
1 parent 14c3115 commit 824419b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import PlausibleProvider from 'next-plausible';
import { ThemeProvider } from 'next-themes';
import posthog from 'posthog-js';
import { PostHogProvider } from 'posthog-js/react';
import { type FC, type ReactNode } from 'react';
import { SWRConfig } from 'swr';

Expand Down Expand Up @@ -30,6 +32,14 @@ const CustomizedPlausibleProvider: FC<CustomizedPlausibleProviderProps> = ({
);
};

if (typeof window !== 'undefined' && env.NEXT_PUBLIC_POSTHOG_KEY) {
posthog.init(env.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: '/ingest',
ui_host: 'https://eu.posthog.com',
person_profiles: 'always',
});
}

const swrFetcher = async (url: string) => {
const response = await fetch(url);
if (!response.ok) throw new Error(await response.text());
Expand All @@ -43,7 +53,9 @@ type ProvidersProps = {
export const Providers: FC<ProvidersProps> = ({ children }) => (
<ThemeProvider attribute="class">
<SWRConfig value={{ fetcher: swrFetcher }}>
<CustomizedPlausibleProvider>{children}</CustomizedPlausibleProvider>
<CustomizedPlausibleProvider>
<PostHogProvider client={posthog}>{children}</PostHogProvider>
</CustomizedPlausibleProvider>
</SWRConfig>
</ThemeProvider>
);
2 changes: 2 additions & 0 deletions env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export const env = createEnv({
client: {
NEXT_PUBLIC_PLAUSIBLE_DOMAIN: z.string().optional(),
NEXT_PUBLIC_PLAUSIBLE_HOST: z.string().optional(),
NEXT_PUBLIC_POSTHOG_KEY: z.string().optional(),
},
experimental__runtimeEnv: {
NEXT_PUBLIC_PLAUSIBLE_DOMAIN: process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN,
NEXT_PUBLIC_PLAUSIBLE_HOST: process.env.NEXT_PUBLIC_PLAUSIBLE_HOST,
NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
},
emptyStringAsUndefined: true,
});
2 changes: 2 additions & 0 deletions lib/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { usePlausible } from 'next-plausible';
import posthog from 'posthog-js';
import { useCallback, useMemo } from 'react';

type EmptyEvent = Record<string, never>;
Expand Down Expand Up @@ -27,6 +28,7 @@ export const useAnalytics = () => {
const reportEvent = useCallback(
<T extends keyof Events>(event: T, props: Events[T]) => {
plausible(event, { props });
posthog.capture(event, props);
},
[plausible],
);
Expand Down
14 changes: 14 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ const nextConfig = {
],
formats: ['image/avif', 'image/webp'],
},
rewrites: async () => [
{
source: '/ingest/static/:path*',
destination: 'https://eu-assets.i.posthog.com/static/:path*',
},
{
source: '/ingest/:path*',
destination: 'https://eu.i.posthog.com/:path*',
},
{
source: '/ingest/decide',
destination: 'https://eu.i.posthog.com/decide',
},
],
redirects: async () => [
{
source: '/lookup',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"next-plausible": "^3.12.4",
"next-themes": "^0.4.3",
"postcss": "^8.4.49",
"posthog-js": "^1.202.2",
"punycode": "^2.3.1",
"react": "19.0.0",
"react-dom": "19.0.0",
Expand Down
33 changes: 33 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 824419b

Please sign in to comment.