The official Mixpanel library for Python provides functionalities to track events, manage user profiles (People), and integrate with Mixpanel's feature flagging system. It is currently at version 5.1.0 and maintains an active release cadence, frequently adding new features and ensuring compatibility with modern Python versions.
pip install mixpanelVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the Mixpanel client, track an event, and update user profiles. Ensure your Mixpanel Project Token is set via an environment variable or directly in the code. For applications that terminate quickly (e.g., scripts, serverless functions), explicitly calling `mp.flush()` is crucial to ensure all queued events are sent before the process exits.
Replace `Mixpanel(token, api_host='https://api.mixpanel.com')` with `Mixpanel(token, api_region='US')` or `api_region='EU'` as appropriate.
Upgrade your Python environment to 3.9 or higher, or pin your `mixpanel` dependency to `<4.11.0` (e.g., `pip install 'mixpanel<4.11.0'`).
For critical events, call `mp.flush()` explicitly. For high-throughput scenarios, integrate with an asynchronous task queue (e.g., Celery) or a custom event buffer that flushes periodically.
Upgrade to `mixpanel` version 4.10.1 or higher to benefit from the connection stability fixes.
Replace `Mixpanel(token, api_region='US')` with `Mixpanel(token, api_host='https://api.mixpanel.com')` (or other appropriate `api_host` value) if on `mixpanel<5.0.0`, or upgrade your `mixpanel` dependency to version `5.0.0` or higher.
Ensure that your event properties are correctly formatted (e.g., proper JSON structure, valid data types) and that event timestamps are within Mixpanel's acceptance window (typically within the last 5 days for `track()`, use `import_data()` for older events). Check the `response.text` for specific server error messages.
Convert DataFrame columns or other complex objects to basic JSON-serializable types (strings, numbers, booleans, lists, dictionaries) before passing them to Mixpanel methods. For DataFrames, convert relevant data to a dictionary or list of dictionaries.
Check your network connection and firewall settings. Verify Mixpanel's service status. If making many requests, implement retry logic with exponential backoff or use a `BufferedConsumer` to handle transient network issues and queue events.
Install the Mixpanel Python library using pip: `pip install mixpanel`. If already installed, ensure your Python environment (e.g., virtual environment) is activated and correctly configured.
Always call `mixpanel.identify(distinct_id)` before using `mixpanel.people.set(distinct_id, properties)` to ensure the profile properties are correctly linked to the user. Verify that the `distinct_id` used for `identify()` and `people.set()` consistently matches the user's identifier in Mixpanel.