Official Mixpanel library for Python providing server-side analytics tracking
npx @tessl/cli install tessl/pypi-mixpanel@4.11.0The official Mixpanel library for Python provides comprehensive server-side integration with Mixpanel analytics services. It enables tracking events, managing user profiles, and handling group analytics through a simple and intuitive API, with support for both synchronous and buffered data transmission modes.
pip install mixpanelfrom mixpanel import MixpanelComplete import with all classes and utilities:
from mixpanel import (
Mixpanel, Consumer, BufferedConsumer, MixpanelException,
DatetimeSerializer, json_dumps, __version__
)For custom consumer configurations:
from mixpanel import Mixpanel, Consumer, BufferedConsumerFor error handling:
from mixpanel import Mixpanel, MixpanelExceptionfrom mixpanel import Mixpanel
# Initialize with your project token
mp = Mixpanel("YOUR_PROJECT_TOKEN")
# Track an event
mp.track("user_123", "button_clicked", {
"button_name": "signup",
"page": "homepage"
})
# Set user profile properties
mp.people_set("user_123", {
"$first_name": "John",
"$last_name": "Doe",
"$email": "john@example.com",
"plan": "premium"
})
# Track revenue
mp.people_track_charge("user_123", 29.99, {
"item": "premium_plan",
"currency": "USD"
})The Mixpanel library follows a consumer-based architecture that separates data collection from data transmission:
This design enables flexible data transmission patterns while maintaining a simple API for common use cases.
Track user events with properties and metadata, including support for historical data import for events older than 5 days.
def track(distinct_id: str, event_name: str, properties: dict = None, meta: dict = None): ...
def import_data(api_key: str, distinct_id: str, event_name: str, timestamp: int, properties: dict = None, meta: dict = None, api_secret: str = None): ...Manage user profiles with comprehensive property operations including setting, incrementing, appending to lists, and revenue tracking.
def people_set(distinct_id: str, properties: dict, meta: dict = None): ...
def people_increment(distinct_id: str, properties: dict, meta: dict = None): ...
def people_track_charge(distinct_id: str, amount: float, properties: dict = None, meta: dict = None): ...Create aliases and merge user identities to handle user identification across different stages of the user lifecycle.
def alias(alias_id: str, original: str, meta: dict = None): ...
def merge(api_key: str, distinct_id1: str, distinct_id2: str, meta: dict = None, api_secret: str = None): ...Manage group profiles for organization-level analytics, supporting company profiles, team properties, and group-based segmentation.
def group_set(group_key: str, group_id: str, properties: dict, meta: dict = None): ...
def group_union(group_key: str, group_id: str, properties: dict, meta: dict = None): ...Configure data transmission behavior with direct HTTP consumers or buffered consumers for batch processing.
class Consumer:
def __init__(self, events_url: str = None, people_url: str = None, import_url: str = None, request_timeout: int = None, groups_url: str = None, api_host: str = "api.mixpanel.com", retry_limit: int = 4, retry_backoff_factor: float = 0.25, verify_cert: bool = True): ...
class BufferedConsumer:
def __init__(self, max_size: int = 50, events_url: str = None, people_url: str = None, import_url: str = None, request_timeout: int = None, groups_url: str = None, api_host: str = "api.mixpanel.com", retry_limit: int = 4, retry_backoff_factor: float = 0.25, verify_cert: bool = True): ...class Mixpanel:
def __init__(self, token: str, consumer: Consumer = None, serializer: json.JSONEncoder = DatetimeSerializer):
"""
Create a Mixpanel tracking instance.
Parameters:
- token (str): Your project's Mixpanel token
- consumer (Consumer, optional): Custom consumer for data transmission (default: Consumer())
- serializer (json.JSONEncoder, optional): JSON encoder subclass for serialization (default: DatetimeSerializer)
"""
class MixpanelException(Exception):
"""Raised by consumers when unable to send messages.
This could be caused by a network outage or interruption, or by an invalid
endpoint passed to Consumer.send().
"""
pass
class DatetimeSerializer(json.JSONEncoder):
"""JSON encoder that handles datetime objects by converting them to ISO format strings."""
def default(self, obj):
"""
Convert datetime objects to string format '%Y-%m-%dT%H:%M:%S'.
Parameters:
- obj: Object to serialize
Returns:
str: Formatted datetime string or default JSON encoding
"""
def json_dumps(data, cls: json.JSONEncoder = None) -> str:
"""JSON serialization utility function with compact formatting.
Parameters:
- data: Data to serialize
- cls (json.JSONEncoder, optional): Custom encoder class
Returns:
str: JSON string with separators=(',', ':') for compact output
"""
# Version constants
__version__: str = "4.11.1"
VERSION: str = "4.11.1" # Deprecated, use __version__ instead
# Module logger
logger: logging.Logger # Package logger instance for debugging and monitoring