{"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install mixpanel-py-async"],"cli":null},"imports":["from mixpanel_async import MixpanelAsync","from mixpanel_async import AsyncBufferedConsumer"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}