This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathposthog.tsx
More file actions
93 lines (82 loc) · 2.93 KB
/
posthog.tsx
File metadata and controls
93 lines (82 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { createFileRoute, Link } from '@tanstack/react-router'
import { usePostHog } from '@posthog/react'
import { useState } from 'react'
export const Route = createFileRoute('/demo/posthog')({
component: PostHogDemo,
})
function PostHogDemo() {
const posthog = usePostHog()
const [eventCount, setEventCount] = useState(0)
const posthogKey = import.meta.env.VITE_POSTHOG_KEY
const isConfigured = Boolean(posthogKey) && posthogKey !== 'phc_xxx'
const trackEvent = (
eventName: string,
properties?: Record<string, unknown>
) => {
posthog.capture(eventName, properties)
setEventCount((c) => c + 1)
}
return (
<div className="min-h-screen bg-gray-900 text-white p-8">
<div className="max-w-md mx-auto">
<h1 className="text-3xl font-bold mb-6">PostHog Demo</h1>
{!isConfigured && (
<div className="mb-4 p-4 bg-yellow-900/50 border border-yellow-600 rounded-lg">
<p className="text-yellow-200 text-sm">
<strong>Warning:</strong> VITE_POSTHOG_KEY is not configured.
Events won't be sent to PostHog. Add it to your{' '}
<code className="bg-yellow-900 px-1 rounded">.env</code> file.
</p>
</div>
)}
<div className="bg-gray-800 rounded-lg p-6">
<p className="text-gray-400 mb-4">
Click the button below to send events to PostHog. Check your
PostHog dashboard to see them appear in real-time.
</p>
<button
onClick={() => trackEvent('button_clicked', { button: 'demo' })}
className="w-full bg-cyan-600 hover:bg-cyan-700 px-4 py-3 rounded font-medium"
>
Track Click
</button>
{isConfigured && (
<div className="mt-6 p-4 bg-gray-700 rounded">
<p className="text-sm text-gray-400">Events sent this session:</p>
<p className="text-4xl font-bold text-cyan-400">{eventCount}</p>
</div>
)}
</div>
<p className="mt-4 text-sm text-gray-400">
Open your{' '}
<a
href="https://app.posthog.com/events"
target="_blank"
rel="noopener noreferrer"
className="text-cyan-400 hover:text-cyan-300 underline"
>
PostHog Events
</a>{' '}
page to see these events appear.
</p>
<p className="mt-2 text-sm text-gray-400">
Learn more in the{' '}
<a
href="https://posthog.com/docs/libraries/react"
target="_blank"
rel="noopener noreferrer"
className="text-cyan-400 hover:text-cyan-300 underline"
>
PostHog React docs
</a>
.
</p>
<div className="mt-8">
<Link to="/" className="text-cyan-400 hover:text-cyan-300">
← Back to Home
</Link>
</div>
</div>
</div>
)
}