This repository provides a comprehensive layer on top of the official Python library from Mixpanel which adds:
- Support for anonymizing email addresses and including them in tracked events
- Django middleware & view support (including support for HTMX)
- Unit tested & providing type hints
You can install the package, and then start tracking straight away:
from py_mixpanel import Tracking
tracker = Tracking(mixpanel_token="s3cret")
tracker.track("user@email.com", "Login", {"browser": "Chrome"})If you use Django, you may also:
- Use the
DjangoMixpanelMiddlewareclass to automatically track requests - Use the
DjangoMixpanelMixinto add a user-friendlymixpanel_track()method to any class-based view
These integrations are configured through your own settings.py:
MIXPANEL_OPTIONS = {
# Your Mixpanel token
TOKEN: str,
# Potentially override the API host
API_HOST: str,
# Enable or disable actual tracking
ENABLE_TRACKING: bool, # default: True
# Track certain properties for every user using the middleware
# (uses the session)
USER_TRACKING: bool, # default: False
# Keep track of HTMX headers in fired events
TRACK_HTMX: bool, # default: True
# Name the event that will be sent on every page view
PAGE_VIEW_EVENT_NAME: str, # default: "Page Viewed"
# Provide a static default payload for all events
PAGE_VIEW_EVENT_PAYLOAD: dict, # default: {}
}It is recommended to use uv to develop locally.
Run the tests:
$ uv run pytestRun the type checker:
uv run mypy .Run the formatter:
uv tool run ruff formatRun the linter:
uv tool run ruff check --fix