Skip to content

Instantly share code, notes, and snippets.

@serverhorror
Created June 4, 2011 21:59
Show Gist options
  • Save serverhorror/1008406 to your computer and use it in GitHub Desktop.
Save serverhorror/1008406 to your computer and use it in GitHub Desktop.
# This isn't perfect for high traffic sites, if you need something more production ready
# please tell us or stay tuned as we will open it up shortly.
import urllib
import urllib2
import base64
import simplejson
def track(event, properties=None):
"""
A simple function for asynchronously logging to the mixpanel.com API.
This function requires `curl` and Python version 2.4 or higher.
@param event: The overall event/category you would like to log this data under
@param properties: A dictionary of key-value pairs that describe the event
See http://mixpanel.com/api/ for further detail.
@return str: The data from the response as a str
"""
if properties == None:
properties = {}
token = "YOUR_TOKEN_HERE"
if "token" not in properties:
properties["token"] = token
params = {"event": event, "properties": properties}
data = base64.b64encode(simplejson.dumps(params))
request = "http://api.mixpanel.com/track/?data=" + data
result = urllib2.urlopen(request, data)
return result.read()
# Example usage:
track("invite-friends",
{"method": "email", "number-friends": "12", "ip": "123.123.123.123"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment