-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
961db58
commit 7056fae
Showing
5 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
apps/app/app/(authenticated)/components/posthog-identifier.tsx
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,45 @@ | ||
'use client'; | ||
|
||
import { useUser } from '@clerk/nextjs'; | ||
import { analytics } from '@repo/design-system/lib/analytics/client'; | ||
import { usePathname, useSearchParams } from 'next/navigation'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export const PostHogIdentifier = () => { | ||
const { user } = useUser(); | ||
const identified = useRef(false); | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
|
||
useEffect(() => { | ||
// Track pageviews | ||
if (pathname && analytics) { | ||
let url = window.origin + pathname; | ||
if (searchParams.toString()) { | ||
url = `${url}?${searchParams.toString()}`; | ||
} | ||
analytics.capture('$pageview', { | ||
$current_url: url, | ||
}); | ||
} | ||
}, [pathname, searchParams]); | ||
|
||
useEffect(() => { | ||
if (!user || identified.current) { | ||
return; | ||
} | ||
|
||
analytics.identify(user.id, { | ||
email: user.emailAddresses.at(0)?.emailAddress, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
createdAt: user.createdAt, | ||
avatar: user.imageUrl, | ||
phoneNumber: user.phoneNumbers.at(0)?.phoneNumber, | ||
}); | ||
|
||
identified.current = true; | ||
}, [user]); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
import posthog from 'posthog-js'; | ||
import posthog, { type PostHog } from 'posthog-js'; | ||
|
||
export const analytics = posthog; | ||
const posthogApiKey = process.env.NEXT_PUBLIC_POSTHOG_KEY; | ||
const posthogHost = process.env.NEXT_PUBLIC_POSTHOG_HOST; | ||
|
||
if (!posthogApiKey || !posthogHost) { | ||
throw new Error( | ||
'NEXT_PUBLIC_POSTHOG_KEY or NEXT_PUBLIC_POSTHOG_HOST is not set' | ||
); | ||
} | ||
|
||
export const analytics = posthog.init(posthogApiKey, { | ||
api_host: '/ingest', | ||
ui_host: 'https://us.posthog.com', | ||
person_profiles: 'identified_only', | ||
capture_pageview: false, // Disable automatic pageview capture, as we capture manually | ||
}) as PostHog; |
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