def mp_track_event(request, event_name, properties=None, **kwargs): import re from mixpanel.tasks import EventTracker if not settings.MIXPANEL_ENABLED: return if properties == None: properties = {} if 'distinct_id' not in properties: properties['distinct_id'] = request.COOKIES.get('mpDistinctID', None) properties['campaign'] = request.COOKIES.get('campaign', 'None') if hasattr(settings, 'ANALYTICS_EXCLUSION_TOKEN'): utmv = request.COOKIES.get('__utmv', None) pattern = re.compile(".*?" + settings.ANALYTICS_EXCLUSION_TOKEN + "*") if utmv and pattern.match(utmv): return try: et = EventTracker() args = (event_name, properties) et.delay(*args, **kwargs) except Exception, err: pass
def test_failed_request(self): EventTracker.endpoint = 'brokenurl' with eager_tasks(): result = EventTracker.delay('event_foo') self.assertNotEqual(result.traceback, None)
def test_failed_socket_request(self): def new_read(*args, **kwargs): raise socket.error('BOOM') self.response.read = new_read with eager_tasks(): result = EventTracker.delay('event_foo') self.assertNotEqual(result.traceback, None)
def send_mixpanel_event(event_name, user, event_dict=None): """ Helper function that takes from various views an event name, an event_dict and an user and populates the event dict with a) distinct_id of the user with key 'distinct_id' b) 'username' = user.firstname + user.lastname c) 'age' = in years, today - DOB d) 'gender' = user.gender e) total em f) redeemable em g) time on earthmiles = today - joining date and then calls EventTracker.delay() with the modified dictionary """ from dateutil.relativedelta import relativedelta from datetime import datetime, date if event_dict == None: event_dict = {} try: age = relativedelta(datetime.today(), user.age, ).years days_on_em = (datetime.today() - user.date_joined.replace(tzinfo=None)).days event_dict.update( { 'distinct_id': user.id, 'Username': user.__str__(), 'Age': age, 'Gender': user.gender, 'Days on Earthmiles': days_on_em, 'Total Earthmiles': user.total_earthmiles, 'Redeemable Earthmiles': user.redeemable_earthmiles, } ) except AttributeError as e: event_dict.update( { 'distinct_id': user.id, } ) #Try to send event to mixpanel. If there is an error, it could be because we're testing on localhost, in which case, pass, else raise the error try: EventTracker.delay(event_name, event_dict) except ConnectionError as e: if 'localhost' not in str(e): raise
def send_mixpanel_event(event_name, user, event_dict=None): """ Helper function that takes from various views an event name, an event_dict and an user and populates the event dict with a) distinct_id of the user with key 'distinct_id' b) 'username' = user.firstname + user.lastname c) 'age' = in years, today - DOB d) 'gender' = user.gender e) total em f) redeemable em g) time on earthmiles = today - joining date and then calls EventTracker.delay() with the modified dictionary """ from dateutil.relativedelta import relativedelta from datetime import datetime, date if event_dict == None: event_dict = {} try: age = relativedelta( datetime.today(), user.age, ).years days_on_em = (datetime.today() - user.date_joined.replace(tzinfo=None)).days event_dict.update({ 'distinct_id': user.id, 'Username': user.__str__(), 'Age': age, 'Gender': user.gender, 'Days on Earthmiles': days_on_em, 'Total Earthmiles': user.total_earthmiles, 'Redeemable Earthmiles': user.redeemable_earthmiles, }) except AttributeError as e: event_dict.update({ 'distinct_id': user.id, }) #Try to send event to mixpanel. If there is an error, it could be because we're testing on localhost, in which case, pass, else raise the error try: EventTracker.delay(event_name, event_dict) except ConnectionError as e: if 'localhost' not in str(e): raise
def test_failed_socket_request(self): mp_settings.MIXPANEL_API_SERVER = '127.0.0.1:60000' with eager_tasks(): result = EventTracker.delay('event_foo') self.assertNotEqual(result.traceback, None)
def test_failed_socket_request(self): self.response.read = Mock(side_effect=socket.error('BOOM')) with eager_tasks(): result = EventTracker.delay('event_foo') self.assertNotEqual(result.traceback, None)
def test_failed_request(self): self.response.status = 400 with eager_tasks(): result = EventTracker.delay('event_foo') self.assertNotEqual(result.traceback, None)
def send_event(event_name, properties): if not event_name: raise ValueError("event name is required") EventTracker.delay(event_name, properties)
def send_event(event_name, **kwargs): if not event_name: raise ValueError("event name is required") EventTracker.delay(event_name, **kwargs)