Skip to content

Instantly share code, notes, and snippets.

@ttrefren
Created October 7, 2009 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ttrefren/203916 to your computer and use it in GitHub Desktop.
Save ttrefren/203916 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 subprocess
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 Instance of L{subprocess.Popen}
"""
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
return subprocess.Popen(("curl",request), stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
def track_funnel(funnel, step, goal, properties=None):
if properties == None:
properties = {}
properties["funnel"] = funnel
properties["step"] = step
properties["goal"] = goal
track("mp_funnel", properties)
# Example usage:
track("invite-friends", {"method": "email", "number-friends": "12", "ip": "123.123.123.123"})
# You MUST include a 'distinct_id' OR 'ip' property to use the funnel
track_funnel("signup funnel", 1, "landing page", {"distinct_id": "USER_ID", "referer": "campaign 1"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment