TanStack Start
Contents
This tutorial shows how to integrate PostHog with a TanStack Start app for both client-side and server-side analytics.
Installation
Install the required packages:
@posthog/react- React package for our JS Web SDK for client-side usageposthog-node- PostHog Node.js SDK for server-side event capture
Initialize PostHog on the client
Wrap your app with PostHogProvider in your root route with your project API key, host, and other options.
Once the provider is in place, PostHog automatically captures pageviews, sessions, and web vitals.
Capture events on the client
Use the usePostHog hook from @posthog/react in any component to capture custom events:
Identify users
Call posthog.identify() when a user logs in to link their events to a user ID:
Call posthog.reset() on logout to clear the identified user.
Initialize PostHog on the server
Create a server-side PostHog client using posthog-node. Use a singleton pattern so you reuse the same client across requests:
Capture events on the server
Use the server client in TanStack Start API routes to capture events server-side. Server-side capture is useful for tracking events that shouldn't be spoofable from the client, like purchases or authentication:
The server-side capture call requires a distinctId (the user identifier), an event name, and optional properties.
Next steps
Installing the JS Web SDK and Node SDK means all of their functionality is available in your TanStack Start project. To learn more about this, have a look at our JS Web SDK docs and Node SDK docs.