CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mixpanel

Official Mixpanel library for Python providing server-side analytics tracking

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Mixpanel

The 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.

Package Information

  • Package Name: mixpanel
  • Language: Python
  • Installation: pip install mixpanel

Core Imports

from mixpanel import Mixpanel

Complete 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, BufferedConsumer

For error handling:

from mixpanel import Mixpanel, MixpanelException

Basic Usage

from 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"
})

Architecture

The Mixpanel library follows a consumer-based architecture that separates data collection from data transmission:

  • Mixpanel: Primary class for tracking events and user profiles, delegates sending to consumers
  • Consumer: Sends HTTP requests directly to Mixpanel, one per call (default behavior)
  • BufferedConsumer: Maintains buffers and sends messages in batches for improved performance
  • MixpanelException: Handles communication errors and API failures

This design enables flexible data transmission patterns while maintaining a simple API for common use cases.

Capabilities

Event Tracking

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): ...

Event Tracking

People Analytics

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): ...

People Analytics

Identity Management

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): ...

Identity Management

Group Analytics

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): ...

Group Analytics

Consumer Configuration

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): ...

Consumer Configuration

Types

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

docs

consumer-configuration.md

event-tracking.md

group-analytics.md

identity-management.md

index.md

people-analytics.md

tile.json