Official PostHog Python library to capture and send events to any PostHog instance (including PostHog.com).
This library uses an internal queue to make calls non-blocking and fast. It also batches requests and flushes asynchronously, making it perfect to use in any part of your web app or other server side application that needs performance.
pip install posthog
In your app, import the posthog library and set your api key before making any calls.
import posthog
posthog.api_key = 'YOUR API KEY'
You can find your key in the /setup page in PostHog.
To debug, you can set debug mode.
posthog.debug = True
Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up.
A capture
call requires
distinct id
which uniquely identifies your userevent name
to make sure- We recommend using [verb] [noun], like
movie played
ormovie updated
to easily identify what your events mean later on.
- We recommend using [verb] [noun], like
Optionally you can submit
properties
, which can be a dict with any information you'd like to add
For example:
posthog.capture('distinct id', 'movie played', {'movie_id': '123', 'category': 'romcom'})
Identify lets you add metadata on your users so you can more easily identify who they are in PostHog, and even do things like segment users by these properties.
An identify
call requires
distinct id
which uniquely identifies your userproperties
with a dict with any key: value pairs
For example:
posthog.capture('distinct id', {
'email': 'dwayne@gmail.com',
'name': 'Dwayne Johnson'
})
The most obvious place to make this call is whenever a user signs up, or when they update their information.
To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call. This will allow you to answer questions like "Which marketing channels leads to users churning after a month?" or "What do users do on our website before signing up?"
In a purely back-end implementation, this means whenever an anonymous user does something, you'll want to send a session ID (Django, Flask) with the capture call. Then, when that users signs up, you want to do an alias call with the session ID and the newly created user ID.
The same concept applies for when a user logs in.
An alias
call requires
previous distinct id
the unique ID of the user beforedistinct id
the current unique id
For example:
posthog.alias('anonymous session id', 'distinct id')
For Django, you can do the initialisation of the key in the AppConfig, so that it's available everywhere.
in yourapp/apps.py
from django.apps import AppConfig
import posthog
class YourAppConfig(AppConfig):
def ready(self):
posthog.api_key = 'your key'
Then, anywhere else in your app you can do
import posthog
def homepage(request):
# example capture
posthog.capture(request.session.session_key, 'page view', ....)
As our open source project PostHog shares the same module name, we create a special posthog-analytics
package, mostly for internal use to avoid module collision. It is the exact same.
- Increase
VERSION
inposthog/version.py
- run
make release
andmake release_analytics
git commit -am "Release X.Y.Z."
(where X.Y.Z is the new version)git tag -a X.Y.Z -m "Version X.Y.Z"
(where X.Y.Z is the new version).
This library is largely based on the analytics-python
package.