Phoenix
Contents
PostHog makes it easy to get data about traffic and usage of your Phoenix projects. 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 Phoenix app using the JavaScript Web SDK.
You might also be interested in our Elixir SDK for capturing events from your servers.
Installation
Go to your project settings and copy your web snippet. It looks like this:
With the JavaScript snippet copied, add it to your main layout file, usually located in lib/<app>/templates/layouts/app.html.eex at the bottom of the <head> tag.
LiveView installation
PostHog also supports Phoenix LiveView with a small tweak to your configuration as the JavaScript snippet doesn't track LiveView's navigation by default.
To properly capture LiveView pageviews/navigation, you need to add an phx:navigate event listener to the bottom of the <head> tag in your main layout file,
usually located in lib/<app>/templates/layouts/app.html.eex:
This captures a $pageview event any time the browser's URL bar is programmatically changed by Phoenix or the user.
You can learn more about the phx:navigate event in the Phoenix LiveView documentation.
Capture custom events
To capture custom events, you can use posthog.capture() in your LiveView or regular Phoenix views. Here are some examples:
Basic event capture
Event capture with properties
Capturing events in LiveView
In your LiveView, you can capture events in response to user interactions:
Capturing events in regular Phoenix views
For regular Phoenix views, you can add event tracking to your templates:
Next steps
Installing the JavaScript Web SDK or snippet means all of its functionality is available in your Phoenix project. To learn more about this, have a look at our JavaScript Web SDK docs.