Commits

Anonymous committed 52dbf86

Standardize event submission for analytics backends.

Comments (0)

Files changed (4)

experiments/analytics/base.py

 
     def record(self, goal_record, experiment_user):
         raise NotImplementedError()
+
+    def event(self, name, properties, request=None):
+        raise NotImplementedError()

experiments/analytics/kissmetrics.py

 from experiments.analytics import IdentificationError
 from experiments.analytics.base import BaseAnalytics
 from experiments.models import Participant
+from experiments.utils import WebUser
 
 
 class KissMetrics(BaseAnalytics):
         if self._identify(experiment_user):
             self.KM.record(action='Goal Recorded',
                            props={'Goal Type': unicode(goal_record.goal_type)})
+
+    def event(self, name, properties, request=None):
+        if request and self._identify(WebUser(request)):
+            self.KM.record(action=name, props=properties)

experiments/analytics/mixpanel.py

 from experiments.analytics import IdentificationError
 from experiments.analytics.base import BaseAnalytics
 from experiments.models import Participant
+from experiments.utils import WebUser
 
 
 class Mixpanel(BaseAnalytics):
     def _identify(self, experiment_user):
         self.identity = None
         self.remote_addr = None
-        try:
-            request = experiment_user.request
-        except AttributeError:
-            request = experiment_user
+        request = experiment_user.request
         if hasattr(request, 'META'):
             self.remote_addr = request.META.get('REMOTE_ADDR', None)
         try:
             )
             self.tracker.run(event_name='Goal Recorded',
                              properties=properties)
+
+    def event(self, name, properties, request=None):
+        if request and self._identify(WebUser(request)):
+            properties = self._properties(properties)
+            self.tracker.run(event_name=name, properties=properties)

experiments/tests/test_analytics.py

                                  experiment_user=experiment_user)
                 self.mox.VerifyAll()
 
+        def test_event(self):
+            KM = self.mox.CreateMockAnything()
+            analytics = KissMetrics(KM=KM)
+            with self.web_user(AnonymousUser()) as experiment_user:
+                KM.identify(analytics._id_from_session(experiment_user.session))
+                KM.record(action='Event', props={'Foo': 'Bar'})
+                self.mox.ReplayAll()
+                analytics.event(name='Event',
+                                properties={'Foo': 'Bar'},
+                                request=experiment_user.request)
+                self.mox.VerifyAll()
+
         @contextmanager
         def web_user(self, user):
             session = get_session(None)
                                  experiment_user=experiment_user)
                 self.mox.VerifyAll()
 
+        def test_event(self):
+            import time
+            tracker = self.mox.CreateMockAnything()
+            analytics = Mixpanel(tracker=tracker)
+            now = time.gmtime()
+            self.mox.StubOutWithMock(time, 'gmtime')
+            time.gmtime().AndReturn(now)
+            with self.web_user(AnonymousUser()) as experiment_user:
+                properties = {
+                    'time': '%d' % time.mktime(now),
+                    'distinct_id': ('Session %s' %
+                                    experiment_user.session.session_key),
+                    'Foo': 'Bar'
+                }
+                tracker.run(event_name='Event',
+                            properties=properties)
+                self.mox.ReplayAll()
+                analytics.event(name='Event',
+                                properties={'Foo': 'Bar'},
+                                request=experiment_user.request)
+                self.mox.VerifyAll()
+
         @contextmanager
         def web_user(self, user):
             session = get_session(None)