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