Created
April 2, 2014 03:10
-
-
Save chris-martin/9927355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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