{"id":9949,"library":"mixpanel-py-async","title":"Mixpanel Async Python Library","description":"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.","status":"active","version":"0.3.0","language":"en","source_language":"en","source_url":"https://github.com/jessepollak/mixpanel-python-async","tags":["mixpanel","analytics","async","event tracking"],"install":[{"cmd":"pip install mixpanel-py-async","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"MixpanelAsync","correct":"from mixpanel_async import MixpanelAsync"},{"symbol":"AsyncBufferedConsumer","correct":"from mixpanel_async import AsyncBufferedConsumer"}],"quickstart":{"code":"import asyncio\nimport os\nfrom mixpanel_async import MixpanelAsync\n\nasync def main():\n    # Retrieve Mixpanel project token from environment variables\n    # For a real application, ensure MIXPANEL_PROJECT_TOKEN is set\n    project_token = os.environ.get(\"MIXPANEL_PROJECT_TOKEN\", \"YOUR_MIXPANEL_PROJECT_TOKEN\")\n\n    if project_token == \"YOUR_MIXPANEL_PROJECT_TOKEN\":\n        print(\"Warning: MIXPANEL_PROJECT_TOKEN environment variable not set. Using a placeholder.\")\n\n    # Initialize Mixpanel with the project token. AsyncBufferedConsumer is used by default.\n    mixpanel = MixpanelAsync(project_token)\n\n    try:\n        user_id = \"test_user_123\"\n        # Track an event\n        await mixpanel.track(user_id, \"Product Viewed\", {\"product_id\": \"SKU456\", \"category\": \"Electronics\"})\n\n        # Update user profile properties\n        await mixpanel.people_set(user_id, {\"$first_name\": \"Async\", \"$last_name\": \"User\", \"Plan\": \"Free\"})\n\n        # Increment a user property\n        await mixpanel.people_increment(user_id, {\"Views\": 1})\n\n        print(f\"Tracking data for user {user_id}...\")\n\n    finally:\n        # Crucial: flush any remaining buffered events before the application exits\n        await mixpanel.flush()\n        # Close the consumer's HTTP session and clean up resources\n        await mixpanel.close()\n        print(\"Mixpanel events flushed and consumer closed.\")\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n","lang":"python","description":"This example demonstrates how to initialize `MixpanelAsync`, track an event, update user profile properties, and increment a user property. It highlights the importance of calling `flush()` and `close()` to ensure all buffered events are sent and resources are properly cleaned up before application exit."},"warnings":[{"fix":"Review your `AsyncBufferedConsumer` initialization. Remove any `*args` or `**kwargs` that were previously passed and ensure only named, supported parameters are used. New parameters like `import_url`, `request_timeout`, etc., are now directly supported.","message":"The `AsyncBufferedConsumer` constructor no longer accepts arbitrary `*args` or `**kwargs`. All parameters must be explicitly defined arguments.","severity":"breaking","affected_versions":">=0.3.0"},{"fix":"If you were explicitly passing `async=True` to `flush`, change it to `async_=True`.","message":"The `flush` argument for the `AsyncBufferedConsumer.flush` method was renamed from `async` to `async_` to avoid conflicts with Python 3.7's `async` keyword.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Always call `await mixpanel.flush()` to send any remaining events in the buffer, and `await mixpanel.close()` to clean up resources, before your application terminates. This should typically be done in a `finally` block or a shutdown hook.","message":"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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Remove any non-standard or arbitrary arguments from your `AsyncBufferedConsumer` initialization. Only use the explicitly defined parameters listed in the documentation.","cause":"Attempting to pass arbitrary `*args` or `**kwargs` to the `AsyncBufferedConsumer` constructor, which is no longer supported in v0.3.0+.","error":"TypeError: __init__() got an unexpected keyword argument 'extra_arg'"},{"fix":"Rename the `async` argument to `async_` when calling `flush()` (e.g., `await mixpanel.flush(async_=True)`).","cause":"Using the old `async` keyword argument with the `flush` method after version 0.2.0.","error":"TypeError: flush() got an unexpected keyword argument 'async'"},{"fix":"Ensure 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.","cause":"The `AsyncBufferedConsumer` buffers events, and they may not have been sent before the application closed.","error":"Events are not appearing in Mixpanel, or data is incomplete."}]}