The mixpanel-python library is the official Python SDK for server-side integration with Mixpanel. It provides a comprehensive interface for tracking events, managing user and group profiles, and evaluating feature flags from Python applications running on servers, background workers, or data pipelines.
This document introduces the library's purpose, architecture, and main components. For installation instructions, see Installation. For basic usage patterns, see Quick Start. For detailed API documentation, see API Reference.
The library enables server-side applications to:
/track and /import endpoints/engage endpoint/groups endpointThe SDK supports both synchronous and asynchronous programming models and is compatible with Python 3.9+ and PyPy.
Sources: mixpanel/__init__.py1-16 README.md1-49
The library is organized around three primary subsystems:
The Mixpanel class (mixpanel/__init__.py51-551) serves as the main entry point. Applications instantiate this class with a project token and optional configuration, then call methods to track events and update profiles.
Sources: mixpanel/__init__.py51-79 mixpanel/__init__.py562-676 mixpanel/__init__.py678-781
The consumer architecture implements a pluggable strategy for message transmission:
| Consumer Type | Class | Location | Behavior |
|---|---|---|---|
| Immediate | Consumer | mixpanel/__init__.py562 | Sends each message immediately via HTTP POST |
| Batched | BufferedConsumer | mixpanel/__init__.py678 | Accumulates up to 50 messages before flushing |
The Consumer class (mixpanel/__init__.py562-676) configures HTTP behavior including:
requests.Session (mixpanel/__init__.py616)urllib3.Retry (mixpanel/__init__.py601-614)api_host parameter (mixpanel/__init__.py587)The BufferedConsumer class (mixpanel/__init__.py678-781) wraps Consumer and maintains per-endpoint buffers that flush when they reach capacity or when explicitly requested.
Sources: mixpanel/__init__.py562-676 mixpanel/__init__.py678-781 mixpanel/__init__.py586-618
Feature flag support enables controlled feature rollouts with two complementary evaluation approaches:
Local Provider (mixpanel/flags/local_feature_flags.py):
/flags/definitions endpointLocalFlagsConfig during Mixpanel initialization (mixpanel/__init__.py66-75)Remote Provider (mixpanel/flags/remote_feature_flags.py):
get_variant_value() call makes synchronous API request to /flags endpointRemoteFlagsConfig during Mixpanel initialization (mixpanel/__init__.py77-78)For detailed feature flag documentation, see Feature Flags.
Sources: mixpanel/__init__.py66-78 mixpanel/__init__.py86-98 mixpanel/flags/local_feature_flags.py1 mixpanel/flags/remote_feature_flags.py1
The Mixpanel class exposes methods organized by functionality:
Event Tracking:
track() (mixpanel/__init__.py100-129): Record events with automatic metadata enrichmentimport_data() (mixpanel/__init__.py131-181): Import historical events (>5 days old) requiring API secretProfile Management (People Analytics):
people_set() (mixpanel/__init__.py253-265): Set profile propertiespeople_increment() (mixpanel/__init__.py282-296): Increment numeric propertiespeople_append() (mixpanel/__init__.py298-313): Append to list propertiespeople_track_charge() (mixpanel/__init__.py369-386): Track revenuepeople_set_once(), people_union(), people_unset(), people_remove(), people_delete(), people_clear_charges()Group Analytics:
group_set() (mixpanel/__init__.py418-432): Set group profile propertiesgroup_union() (mixpanel/__init__.py451-468): Merge list propertiesgroup_remove() (mixpanel/__init__.py483-499): Remove list valuesgroup_set_once(), group_unset(), group_delete()Identity Management:
alias() (mixpanel/__init__.py183-213): Create user alias mappingmerge() (mixpanel/__init__.py215-251): Merge two distinct_idsFor complete API documentation, see API Reference.
Sources: mixpanel/__init__.py100-532
All events and profile updates automatically receive metadata:
| Property | Purpose | Generated By |
|---|---|---|
token | Project identifier | self._token (mixpanel/__init__.py114) |
time / $time | Timestamp | self._now() (mixpanel/__init__.py80-81) |
$insert_id | Deduplication UUID | self._make_insert_id() (mixpanel/__init__.py83-84) |
mp_lib | Library identifier | Constant "python" (mixpanel/__init__.py118) |
$lib_version | Library version | __version__ (mixpanel/__init__.py33 mixpanel/__init__.py119) |
The DatetimeSerializer class (mixpanel/__init__.py37-43) extends json.JSONEncoder to handle Python datetime objects by formatting them as ISO 8601 strings.
Sources: mixpanel/__init__.py113-120 mixpanel/__init__.py37-43 mixpanel/__init__.py80-84
The following diagram illustrates the complete flow when tracking an event:
Key Flow Steps:
Metadata Enrichment (mixpanel/__init__.py113-120): The Mixpanel.track() method builds an all_properties dictionary containing the project token, current timestamp, UUID for deduplication, and library metadata.
Serialization (mixpanel/__init__.py46-48 mixpanel/__init__.py129): The event dictionary is serialized to JSON using json_dumps() with the configured DatetimeSerializer.
Consumer Dispatch (mixpanel/__init__.py129): The serialized message is sent to the consumer's send() method with endpoint 'events'.
Transmission:
Consumer (mixpanel/__init__.py619-636): Immediately posts to the API endpointBufferedConsumer (mixpanel/__init__.py724-755): Accumulates messages and flushes when buffer reaches max_size (default 50)HTTP Request (mixpanel/__init__.py638-674): The requests.Session posts with retry logic, TLS verification, and timeout control.
Sources: mixpanel/__init__.py100-129 mixpanel/__init__.py619-676 mixpanel/__init__.py724-780
The library raises MixpanelException (mixpanel/__init__.py553-559) in the following scenarios:
The urllib3.Retry configuration (mixpanel/__init__.py606-614) automatically retries on:
Sources: mixpanel/__init__.py553-559 mixpanel/__init__.py606-674
The Mixpanel class implements both synchronous and asynchronous context manager protocols:
Synchronous:
__enter__() (mixpanel/__init__.py534-535): Returns self__exit__() (mixpanel/__init__.py537-541): Cleans up feature flag providersAsynchronous:
__aenter__() (mixpanel/__init__.py543-544): Returns self__aexit__() (mixpanel/__init__.py546-550): Asynchronously cleans up feature flag providersThis enables resource management patterns:
Sources: mixpanel/__init__.py534-550
5.1.0 (mixpanel/__init__.py33)requests (mixpanel/__init__.py23): HTTP client with connection poolingurllib3 (mixpanel/__init__.py25): Low-level HTTP library for retry logicFor testing infrastructure, see Testing. For release history, see Release History.
Sources: mixpanel/__init__.py23-25 mixpanel/__init__.py33 README.md1-7
Refresh this wiki
This wiki was recently refreshed. Please wait 1 day to refresh again.