Hono
Contents
PostHog makes it easy to get data about usage of your Hono app. Integrating PostHog into your app enables analytics, custom events capture, feature flags, and more.
This guide walks you through integrating PostHog with your Hono app running on the Node.js runtime, but has also been tested with Cloudflare Workers, Vercel, and Bun. You can hook into the same middleware and onError
for other runtimes to capture events.
Prerequisites
To follow this guide along, you need:
- A PostHog instance (either Cloud or self-hosted)
- A Hono application. We suggest trying first with a fresh Hono Node.js starter template.
Installation
Start by installing the posthog-node
package using your package manager.
Then, create a middleware to capture events for your routes. Remember to export your project API key as an environment variable.
Make sure to always call posthog.shutdown()
after capturing events from the server-side. PostHog queues events into larger batches. This call forces all batched events to be flushed immediately.
Next, apply this middleware to your app. This middleware will be called on every request handled by Hono, denoted by the wildcard *
.
In your routes, you can pass information to be captured by your middleware using context set()
and get()
pair
Cloudflare Pages uses its own middleware system that is different from Hono's middleware. Follow the Hono documentation on Cloudflare Pages to handle middleware.
Error tracking
Hono uses app.onError
for root-level error handling. You can take advantage of this for Error tracking.
Next steps
To read more about how to integrate specific PostHog features into Hono, have a look at our Node SDK docs for concepts such as: