Mixpanel Async Python Library
A Python library designed for asynchronously sending event data to Mixpanel, offering an `AsyncBufferedConsumer` for efficient batching and transmission. It streamlines event tracking in asynchronous applications. Currently at version 0.3.0, it receives updates as needed.
Common errors
-
TypeError: __init__() got an unexpected keyword argument 'extra_arg'
cause Attempting to pass arbitrary `*args` or `**kwargs` to the `AsyncBufferedConsumer` constructor, which is no longer supported in v0.3.0+.fixRemove any non-standard or arbitrary arguments from your `AsyncBufferedConsumer` initialization. Only use the explicitly defined parameters listed in the documentation. -
TypeError: flush() got an unexpected keyword argument 'async'
cause Using the old `async` keyword argument with the `flush` method after version 0.2.0.fixRename the `async` argument to `async_` when calling `flush()` (e.g., `await mixpanel.flush(async_=True)`). -
Events are not appearing in Mixpanel, or data is incomplete.
cause The `AsyncBufferedConsumer` buffers events, and they may not have been sent before the application closed.fixEnsure that `await mixpanel.flush()` and `await mixpanel.close()` are called before your application fully exits. Wrap your event tracking logic in a `try...finally` block to guarantee these calls.
Warnings
- breaking The `AsyncBufferedConsumer` constructor no longer accepts arbitrary `*args` or `**kwargs`. All parameters must be explicitly defined arguments.
- breaking The `flush` argument for the `AsyncBufferedConsumer.flush` method was renamed from `async` to `async_` to avoid conflicts with Python 3.7's `async` keyword.
- gotcha Events tracked with `MixpanelAsync` are buffered by default. If the application exits before the buffer is full or the flush interval passes, events might be lost.
Install
-
pip install mixpanel-py-async
Imports
- MixpanelAsync
from mixpanel_async import MixpanelAsync
- AsyncBufferedConsumer
from mixpanel_async import AsyncBufferedConsumer
Quickstart
import asyncio
import os
from mixpanel_async import MixpanelAsync
async def main():
# Retrieve Mixpanel project token from environment variables
# For a real application, ensure MIXPANEL_PROJECT_TOKEN is set
project_token = os.environ.get("MIXPANEL_PROJECT_TOKEN", "YOUR_MIXPANEL_PROJECT_TOKEN")
if project_token == "YOUR_MIXPANEL_PROJECT_TOKEN":
print("Warning: MIXPANEL_PROJECT_TOKEN environment variable not set. Using a placeholder.")
# Initialize Mixpanel with the project token. AsyncBufferedConsumer is used by default.
mixpanel = MixpanelAsync(project_token)
try:
user_id = "test_user_123"
# Track an event
await mixpanel.track(user_id, "Product Viewed", {"product_id": "SKU456", "category": "Electronics"})
# Update user profile properties
await mixpanel.people_set(user_id, {"$first_name": "Async", "$last_name": "User", "Plan": "Free"})
# Increment a user property
await mixpanel.people_increment(user_id, {"Views": 1})
print(f"Tracking data for user {user_id}...")
finally:
# Crucial: flush any remaining buffered events before the application exits
await mixpanel.flush()
# Close the consumer's HTTP session and clean up resources
await mixpanel.close()
print("Mixpanel events flushed and consumer closed.")
if __name__ == "__main__":
asyncio.run(main())