Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Created April 2, 2014 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/9927355 to your computer and use it in GitHub Desktop.
Save chris-martin/9927355 to your computer and use it in GitHub Desktop.
from mixpanel import Mixpanel
import json
class MixpanelProject(object):
def __init__(self, token):
self.mixpanel = Mixpanel(token)
def cookie_name(self):
return 'mp_%s_mixpanel' % self.token
def get_distinct_id_from_request(self, request):
try:
cookie = request.COOKIES.get(self.cookie_name())
return json.loads(cookie)['distinct_id']
except:
return None
def alias_request(self, alias_id, request):
original = self.get_distinct_id_from_request(request)
if original is not None:
self.mixpanel.alias(alias_id=alias_id, original=original)
def track(self, distinct_id, event_name, properties=None, meta=None):
self.mixpanel.track(
distinct_id=distinct_id,
event_name=event_name,
properties=properties or {},
meta=meta or {},
)
def track_request(self, request, event_name, properties=None, meta=None):
distinct_id = self.get_distinct_id_from_request(request)
if distinct_id is not None:
self.track(
distinct_id=distinct_id,
event_name=event_name,
properties=properties or {},
meta=meta or {},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment