-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PostHog analytics integration (#2844)
* Add PostHog analytics integration * chore: update .cspell.json * build(deps): update yarn.lock * chore: update .cspell.json
- Loading branch information
1 parent
6c57a90
commit 46be66d
Showing
8 changed files
with
125 additions
and
22 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
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,29 @@ | ||
'use client'; | ||
|
||
import { usePathname, useSearchParams } from 'next/navigation'; | ||
import { useEffect } from 'react'; | ||
import { usePostHog } from 'posthog-js/react'; | ||
import { POSTHOG_HOST, POSTHOG_KEY } from '@app/constants'; | ||
|
||
export default function PostHogPageView(): null { | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
const posthog = usePostHog(); | ||
|
||
useEffect(() => { | ||
if (!POSTHOG_KEY.value || !POSTHOG_HOST.value) return; | ||
|
||
// Track pageviews | ||
if (pathname && posthog) { | ||
let url = window.origin + pathname; | ||
if (searchParams.toString()) { | ||
url = url + `?${searchParams.toString()}`; | ||
} | ||
posthog.capture('$pageview', { | ||
$current_url: url | ||
}); | ||
} | ||
}, [pathname, searchParams, posthog]); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use client'; | ||
|
||
import { POSTHOG_HOST, POSTHOG_KEY } from '@app/constants'; | ||
import posthog from 'posthog-js'; | ||
import { PostHogProvider } from 'posthog-js/react'; | ||
|
||
const key = POSTHOG_KEY.value; | ||
const host = POSTHOG_HOST.value; | ||
|
||
if (typeof window !== 'undefined' && key && host) { | ||
posthog.init(key, { | ||
api_host: host, | ||
person_profiles: 'identified_only', | ||
capture_pageview: false, | ||
capture_pageleave: true | ||
}); | ||
} | ||
|
||
export function PHProvider({ children }: { children: React.ReactNode }) { | ||
if (!key || !host) { | ||
return <>{children}</>; | ||
} | ||
|
||
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 |
---|---|---|
|
@@ -14546,6 +14546,11 @@ fd-slicer@~1.1.0: | |
dependencies: | ||
pend "~1.2.0" | ||
|
||
fflate@^0.4.8: | ||
version "0.4.8" | ||
resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae" | ||
integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA== | ||
|
||
[email protected], figures@^3.0.0, figures@^3.2.0: | ||
version "3.2.0" | ||
resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af" | ||
|
@@ -21715,6 +21720,15 @@ postgres-range@^1.1.1: | |
resolved "https://registry.yarnpkg.com/postgres-range/-/postgres-range-1.1.3.tgz#9ccd7b01ca2789eb3c2e0888b3184225fa859f76" | ||
integrity sha512-VdlZoocy5lCP0c/t66xAfclglEapXPCIVhqqJRncYpvbCgImF0w67aPKfbqUMr72tO2k5q0TdTZwCLjPTI6C9g== | ||
|
||
posthog-js@^1.154.5: | ||
version "1.154.5" | ||
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.154.5.tgz#1737ce0b31611ae291c3c301f356ec69b835c354" | ||
integrity sha512-YYhWckDIRObfCrQpiLq+fdcDTIbQp8ebiKi0ueGohMRgugIG9LJVSpBgCeCHZm2C7sOxDUNcAr3T5VBDUSQoOg== | ||
dependencies: | ||
fflate "^0.4.8" | ||
preact "^10.19.3" | ||
web-vitals "^4.0.1" | ||
|
||
[email protected]: | ||
version "5.2.3" | ||
resolved "https://registry.yarnpkg.com/preact-render-to-string/-/preact-render-to-string-5.2.3.tgz#23d17376182af720b1060d5a4099843c7fe92fe4" | ||
|
@@ -21727,6 +21741,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/preact/-/preact-10.11.3.tgz#8a7e4ba19d3992c488b0785afcc0f8aa13c78d19" | ||
integrity sha512-eY93IVpod/zG3uMF22Unl8h9KkrcKIRs2EGar8hwLZZDU1lkjph303V9HZBwufh2s736U6VXuhD109LYqPoffg== | ||
|
||
preact@^10.19.3: | ||
version "10.23.1" | ||
resolved "https://registry.yarnpkg.com/preact/-/preact-10.23.1.tgz#d400107289bc979881c5212cb5f5cd22cd1dc38c" | ||
integrity sha512-O5UdRsNh4vdZaTieWe3XOgSpdMAmkIYBCT3VhQDlKrzyCm8lUYsk0fmVEvoQQifoOjFRTaHZO69ylrzTW2BH+A== | ||
|
||
preact@~10.12.1: | ||
version "10.12.1" | ||
resolved "https://registry.yarnpkg.com/preact/-/preact-10.12.1.tgz#8f9cb5442f560e532729b7d23d42fd1161354a21" | ||
|
@@ -25908,6 +25927,11 @@ wcwidth@^1.0.0, wcwidth@^1.0.1: | |
dependencies: | ||
defaults "^1.0.3" | ||
|
||
web-vitals@^4.0.1: | ||
version "4.2.2" | ||
resolved "https://registry.yarnpkg.com/web-vitals/-/web-vitals-4.2.2.tgz#e883245180b95e175eb75a5ca8903b1a11597d7a" | ||
integrity sha512-nYfoOqb4EmElljyXU2qdeE76KsvoHdftQKY4DzA9Aw8DervCg2bG634pHLrJ/d6+B4mE3nWTSJv8Mo7B2mbZkw== | ||
|
||
webidl-conversions@^3.0.0: | ||
version "3.0.1" | ||
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" | ||
|