-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: init posthog * chore(env): add posthog env variables * feat(analytics/posthog): capture events * chore: enable corepack --------- Co-authored-by: withyellow <[email protected]>
- Loading branch information
Showing
7 changed files
with
137 additions
and
88 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
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,35 @@ | ||
'use client' | ||
|
||
import { usePathname, useSearchParams } from 'next/navigation' | ||
import posthog from 'posthog-js' | ||
import { PostHogProvider } from 'posthog-js/react' | ||
import { useEffect } from 'react' | ||
|
||
if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY) { | ||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, { | ||
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST, | ||
capture_pageview: false, // Disable automatic pageview capture, as we capture manually | ||
}) | ||
} | ||
|
||
export function PostHogPageview() { | ||
const pathname = usePathname() | ||
const searchParams = useSearchParams() | ||
// Track pageviews | ||
useEffect(() => { | ||
if (pathname) { | ||
let url = window.origin + pathname | ||
if (searchParams && searchParams.toString()) { | ||
url = url + `?${searchParams.toString()}` | ||
} | ||
posthog.capture('$pageview', { | ||
$current_url: url, | ||
}) | ||
} | ||
}, [pathname, searchParams]) | ||
return <></> | ||
} | ||
|
||
export function PHProvider({ children }: { children: React.ReactNode }) { | ||
return <PostHogProvider client={posthog}>{children}</PostHogProvider> | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"private": true, | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"build": "next build", | ||
"dev": "next dev --port 3333", | ||
|
@@ -32,6 +33,7 @@ | |
"next-intl": "2.14.0-beta.4", | ||
"next-sanity": "^4.3.2", | ||
"next-themes": "^0.2.1", | ||
"posthog-js": "^1.87.6", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-hook-form": "^7.43.9", | ||
|
Oops, something went wrong.