PostHog makes it easy to get data about traffic and usage of your Django app. Integrating PostHog enables analytics, custom events capture, feature flags, and more.
This guide walks you through integrating PostHog into your Django app using the Python SDK.
Installation
To start, run pip install posthog to install PostHog’s Python SDK.
Then, set the PostHog API key and host in your AppConfig in your your_app/apps.py so that's it's available everywhere:
You can find your project API key and instance address in your project settings.
Next, if you haven't done so already, make sure you add your AppConfig to your settings.py under INSTALLED_APPS:
Lastly, to access PostHog in any file, simply import posthog and call the method you'd like. For example, to capture an event:
Error tracking
If you're using Django 4.2+, you can enable the exception autocapture integration which will also automatically capture Django errors.
For older versions of Django, you will need to instrument your views individually to capture unhandled exceptions:
Next steps
For any technical questions for how to integrate specific PostHog features into Django (such as analytics, feature flags, A/B testing, etc.), have a look at our Python SDK docs.
Alternatively, the following tutorials can help you get started: