Starting using PostHog with Discourse

PostHog works as a replacement for Google Analytics if you don’t want to share your visitors’ data with Google. It also adds a lot of other features such as being able to capture session recordings and JavaScript events out of the box. After you’ve set it up, you can use a flavor of SQL to analyze user behavior.

Get an account on PostHog

The first step is to sign up for an account. You won’t need to provide a credit card at this stage.[1]

The next screen collects your name and the name of your organization:

Collecting your name and organization

After verifying your email, PostHog puts you in your new organization’s dashboard.

Create a project

You’ll want to organize your work into projects. This could be dev, testing, and production or it could be divided by client. If you have more than one server (say, Discourse and WordPress), best practice is to put them in the same project if they have the same set of visitors. To get you started, PostHog creates a Default project, which can be renamed by selecting it from the dropdown menu.

Project dropdown

If you add a credit card to upgrade, you can create up to 6 projects. I’m creating a new project for a client who uses Discourse to host her community for people who love dogs:

Creating a K9 project under my organization: Civitas

After creating the project, PostHog gives you several options for what you can do with it.

Select “Product Analytics” to guide you through installing the PostHog HTML snippet.

Copy this snippet to paste into the right spot on Discourse.

Create a Theme Component on Discourse

The most flexible way to install an HTML snippet on Discourse is to create a Theme Component. That way the code can be shared across all the themes in Discourse and removed if you ever decide to stop using PostHog. As an admin on Discourse, navigate to Admin|Customize|Themes and select the Install button. In the Install dialog box, select Create new, fill in “PostHog” as the Name and select the “Component” Type.

Create a new Theme Component

You’ll want to use the “Add all themes” link to make sure the new component is used by everyone no matter which theme they picked. Then select the Edit CSS/HTML button.

Theme Component settings

Make sure you are editing the Common Head section, which installs the HTML snippet for both Desktop and Mobile users. Paste the HTML snippet for the matching PostHog project and select the Save button.

Edit Common Head

Test the HTML snippet

Once you’ve saved the snippet in the Theme Component, included the component in all themes and saved the new component, data from visitors to Discourse should be collected automatically. To make sure everything is working, browse a few pages without using an ad blocker. Go back to the PostHog dashboard and select the Check installation button.

Check installation

Configure the project

From there you can choose from a variety of per-project options.

By default Enable session recordings is turned off. The free tier allows 5k session recordings a month, so you might hit the cap quickly if you have a very active site. Fortunately you can limit spending per feature and the cost per recording is reasonable. It can be useful to sample sessions by device or channel type to see how people are interacting with the page.

There’s usually no reason not to leave the other options enabled.


  1. Or ever if you don’t need paid features such as multiple projects. Even then, it’s possible to limit your spend to $0. ↩︎

1 Like