jessepollak / mixpanel-python-async

:zap: Batch and send your Mixpanel API calls asynchronously in Python
Other
59 stars 16 forks source link

mixpanel-python-async

This library allows for using the Mixpanel python client in an asynchronous way. Using the AsyncBufferedConsumer, events sent to the Mixpanel API will be batched and then flushed in a thread without blocking the main thread. This is extremely useful in a request/response scenario where response time is important.

This library was originally created for use at Clef and is in production use there.

Installation

The library can be installed using pip:

pip install mixpanel-py-async

Getting Started

Typical usage usually looks like this:

#!/usr/bin/env python
from mixpanel import Mixpanel
from mixpanel_async import AsyncBufferedConsumer

mp = Mixpanel(YOUR_TOKEN, consumer=AsyncBufferedConsumer())

# tracks an event with certain properties
mp.track('distinct_id', 'event name', {'color' : 'blue', 'size': 'large'})

# sends an update to a user profile
mp.people_set(USER_ID, {'$first_name' : 'Amy', 'favorite color': 'red'})

These events will be batched and then sent in a seperate, asynchronous thread.

Configuration

For most users, the default configuration should work perfectly. For more specific needs, AsyncBufferedConsumer has a variety of configuration options, which you can use to manage how it batches and sends API requests.

Usage

Typically, after configuring the AsyncBufferedConsumer and passing it to the Mixpanel object, you will never have to use it again. That said, there are a few methods which can be useful.

#!/usr/bin/env python
from mixpanel import Mixpanel
from mixpanel_async import AsyncBufferedConsumer

consumer = AsyncBufferedConsumer()
mp = Mixpanel(YOUR_TOKEN, consumer=consumer)

# tracks an event with certain properties
mp.track('distinct_id', 'event name', {'color' : 'blue', 'size': 'large'})

consumer.flush(async_=False)
# all events are flushed and process ends

Bugs

If you find an issue, let us know in the issues section!

Contributing

From the Rubinius contribution page:

Writing code and participating should be fun, not an exercise in perseverance. Stringent commit polices, for whatever their other qualities may bring, also mean longer turnaround times.

Submit a patch and once it's accepted, you'll get commit access to the repository. Feel free to fork the repository and send a pull request, once it's merged in you'll get added. If not, feel free to bug jessepollak about it.

How To Contribute

Once you're ready:

Once it's accepted:

git remote set-url origin git@github.com:jessepollak/mixpanel-python-async.git

Otherwise, you can continue to hack away in your own fork.

thanks to rubygems for inspiration of our guidelines

Additional Information

Mixpanel python docs Mixpanel client libary