-
Notifications
You must be signed in to change notification settings - Fork 52
feat: posthog #472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: posthog #472
Changes from 1 commit
2dc727a
d1f66c2
588952e
1025dae
43384c8
965d52c
259c870
e33b4fb
6dd2f88
8a352f7
805a02d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,19 +89,56 @@ export interface BridgeClickCount { | |
| count: number; | ||
| } | ||
|
|
||
| async function getBridgeClickedCountsFromDb(): Promise<BridgeClickCount[]> { | ||
| const bridgeClickCounts = await database | ||
| .select({ | ||
| bridgeId: sql<string>`properties->>'bridgeId'`, | ||
| count: sql<number>`count(distinct "sessionId")::int`, | ||
| }) | ||
| .from(analyticsEventsTable) | ||
| .where(eq(analyticsEventsTable.eventName, 'bridge_clicked')) | ||
| .groupBy(sql`properties->>'bridgeId'`) | ||
| .orderBy(sql`count(distinct "sessionId") desc`); | ||
|
|
||
| return bridgeClickCounts; | ||
| } | ||
|
|
||
| async function getBridgeClickedCountsFromPostHog(): Promise<BridgeClickCount[]> { | ||
| const res = await fetch( | ||
| `https://us.posthog.com/api/projects/${process.env.POSTHOG_PROJECT_ID}/query`, | ||
| { | ||
| method: 'POST', | ||
| headers: { | ||
| Authorization: `Bearer ${process.env.POSTHOG_API_KEY}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| query: { | ||
| kind: 'HogQLQuery', | ||
| query: ` | ||
| SELECT properties.bridgeId, count() AS cnt | ||
|
shazarre marked this conversation as resolved.
Outdated
|
||
| FROM events | ||
| WHERE event = 'bridge_clicked' | ||
| GROUP BY properties.bridgeId | ||
| ORDER BY cnt DESC | ||
| `, | ||
| }, | ||
| }), | ||
| // Cache the response for an hour | ||
| next: { revalidate: 3600 }, | ||
| }, | ||
| ); | ||
|
|
||
| const data = await res.json(); | ||
| return data.results.map(([bridgeId, count]: [string, number]) => ({ bridgeId, count })); | ||
| } | ||
|
|
||
| export async function getBridgeClickedCounts(): Promise<BridgeClickCount[]> { | ||
| const source = process.env.BRIDGE_COUNTS_SOURCE ?? 'database'; | ||
| try { | ||
| const bridgeClickCounts = await database | ||
| .select({ | ||
| bridgeId: sql<string>`properties->>'bridgeId'`, | ||
| count: sql<number>`count(distinct "sessionId")::int`, | ||
| }) | ||
| .from(analyticsEventsTable) | ||
| .where(eq(analyticsEventsTable.eventName, 'bridge_clicked')) | ||
| .groupBy(sql`properties->>'bridgeId'`) | ||
| .orderBy(sql`count(distinct "sessionId") desc`); | ||
|
|
||
| return bridgeClickCounts; | ||
| return await (source === 'posthog' | ||
| ? getBridgeClickedCountsFromPostHog() | ||
| : getBridgeClickedCountsFromDb()); | ||
|
Comment on lines
+64
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error fetching bridge click counts:', error); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import { usePostHog } from '@posthog/react'; | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { AnalyticsEventMap, AnalyticsEventName } from 'src/types/analytics'; | ||
| import { v4 as uuidv4, validate as validateUUID } from 'uuid'; | ||
| import { trackEvent } from './analytics'; | ||
|
|
||
| const SESSION_STORAGE_KEY = 'analytics_session_id'; | ||
|
|
||
| // React hook for analytics tracking with session management | ||
| export function useTrackEvent() { | ||
| const posthog = usePostHog(); | ||
| const [sessionId, setSessionId] = useState<string>(''); | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -40,8 +41,9 @@ export function useTrackEvent() { | |
| const track = useCallback( | ||
| <T extends AnalyticsEventName>(eventName: T, properties: AnalyticsEventMap[T]) => { | ||
| trackEvent(eventName, properties, sessionId); | ||
| posthog?.capture(eventName, properties); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| }, | ||
| [sessionId], | ||
| [sessionId, posthog], | ||
| ); | ||
|
|
||
| return track; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.