PostHog makes it easy to get data about traffic and usage of your Remix app. Integrating PostHog into your site enables analytics about user behavior, custom events capture, session recordings, feature flags, and more.
This guide walks you through integrating PostHog into your Remix app using the JavaScript Web SDK.
Installation
Install posthog-js
using your package manager:
yarn add posthog-js# ornpm install --save posthog-js
Then, go to app/entry.client.tsx
and initialize PostHog as a component. You'll need both your API key and instance address (you can find these in your project settings).
import { RemixBrowser } from "@remix-run/react";import { startTransition, StrictMode, useEffect } from "react";import { hydrateRoot } from "react-dom/client";import posthog from "posthog-js";function PosthogInit() {useEffect(() => {posthog.init('<ph_project_api_key>', {api_host: https://us.i.posthog.com,});}, []);return null;}startTransition(() => {hydrateRoot(document,<StrictMode><RemixBrowser /><PosthogInit/></StrictMode>);});
Next steps
For any technical questions for how to integrate specific PostHog features into Remix (such as analytics, feature flags, A/B testing, surveys, etc.), have a look at our JavaScript Web SDK docs.
Alternatively, the following tutorials can help you get started: