-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(saas): Implement Analytics with PostHog (#19)
## feat: Implement Analytics with PostHog This pull request integrates PostHog for detailed user and application analytics. **Key changes:** * **PostHog integration:** Added `posthog-js` as a dependency and configured it to capture events and user data. * **User identification:** Authenticated users are automatically identified in PostHog using their ID and email. * **Page view tracking:** Page views are automatically tracked, providing insights into user navigation. * **Rewrites for PostHog API:** Implemented rewrites to handle PostHog API requests efficiently. * **Admin panel updates:** Added an "Analytics" link to the admin sidebar navigation, directing users to the PostHog dashboard. * **Environment variables:** Added `NEXT_PUBLIC_POSTHOG_KEY` and `NEXT_PUBLIC_POSTHOG_HOST` to the `.env.example` file for configuration. * **Provider setup:** Created a `PosthogProvider` component to manage PostHog initialization and user identification. * **Removed Vercel Analytics:** Replaced the existing Vercel Analytics integration with PostHog. **Additional notes:** * This implementation provides a basic foundation for analytics. Further customization and event tracking can be added as needed. * The PostHog project ID and dashboard URL should be updated in the `siteUrls` configuration. **Testing:** * Verified that events are being captured correctly in the PostHog dashboard. * Tested user identification and page view tracking functionality.
- Loading branch information
Showing
11 changed files
with
131 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"use client"; | ||
|
||
import { env } from "@/env"; | ||
import { useSession } from "next-auth/react"; | ||
import { usePathname } from "next/navigation"; | ||
import posthog from "posthog-js"; | ||
import { PostHogProvider as CSPostHogProvider } from "posthog-js/react"; | ||
import { useEffect } from "react"; | ||
|
||
if (typeof window !== "undefined") { | ||
posthog.init(env.NEXT_PUBLIC_POSTHOG_KEY, { | ||
api_host: "/ingest", | ||
rate_limiting: { | ||
events_burst_limit: 10, | ||
events_per_second: 5, | ||
}, | ||
loaded: (posthog) => { | ||
if (env.NODE_ENV === "development") posthog.debug(); | ||
}, | ||
}); | ||
} | ||
|
||
type PostHogProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export function PosthogProvider({ children }: PostHogProviderProps) { | ||
return ( | ||
<> | ||
<CapturePageviewClient captureOnPathChange={true} /> | ||
<CSPostHogProvider client={posthog}> | ||
<PosthogAuthWrapper>{children}</PosthogAuthWrapper> | ||
</CSPostHogProvider> | ||
</> | ||
); | ||
} | ||
|
||
function PosthogAuthWrapper({ children }: PostHogProviderProps) { | ||
const { data: session, status } = useSession(); | ||
|
||
useEffect(() => { | ||
if (status === "authenticated") { | ||
posthog.identify(session.user.id, { | ||
email: session.user.email, | ||
name: session.user.name, | ||
}); | ||
} else if (status === "unauthenticated") { | ||
posthog.reset(); | ||
} | ||
}, [session, status]); | ||
|
||
return children; | ||
} | ||
|
||
type CapturePageviewClientProps = { | ||
captureOnPathChange?: boolean; | ||
}; | ||
|
||
export function CapturePageviewClient({ | ||
captureOnPathChange = false, | ||
}: CapturePageviewClientProps) { | ||
const pathname = usePathname(); | ||
|
||
useEffect(() => { | ||
const handleCapturePageview = () => posthog.capture("$pageview"); | ||
|
||
handleCapturePageview(); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [captureOnPathChange ? pathname : undefined]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters