1. Éric Araujo
  2. pyramid_mixpanel

Commits

Max Noel  committed 32add83

* People track: Added support for $append and transactions.

  • Participants
  • Parent commits 9d60192
  • Branches default

Comments (0)

Files changed (2)

File pyramid_mixpanel/__init__.py

View file
     def track(event, properties):
         """Track an event and its properties."""
 
-    def people_track(distinct_id, properties):
+    def people_track(distinct_id, properties, action=u"$set"):
         """Track a user with properties."""
 
 
             log.exception('mixpanel track event exception: event %s; properties: %s',
                           event, properties)
 
-    def people_track(self, distinct_id, properties):
+    def people_track(self, distinct_id, properties, action=u'$set'):
         """Create or update user in Mixpanel People.
 
         *distinct_id* must be a string and *properties* a dictionary.
         so we always send all the information.  This has the side
         effect of possibly updating info that have been refreshed with
         real-time update.
+
+        If *action* is ``"$set"`` (default), any properties you specify
+        will replace existing values in mixpanel's DB.
+
+        If *action* is ``"$append"`` and *properties*'s only key is
+        ``"$transactions"``, the transaction in *properties* will be
+        added to the list of transactions for this user in the mixpanel DB.
+
+        Note that Mixpanel only supports ``"$append"`` for transactions,
+        and that transactions must be of the form
+        ``{"$time": iso_datetime, "$amount": number}``.
         """
         try:
             data = base64.b64encode(json.dumps({
                 u'$token': self.token,
                 u'$distinct_id': distinct_id,
-                u'$set': properties,
+                action: properties,
             }))
 
             response = requests.post('https://api.mixpanel.com/engage/',

File pyramid_mixpanel/tests/unittests/test_pyramid_mixpanel.py

View file
                                        {'data': data})
 
     @mock.patch('requests.post')
+    def test_people_track_transaction(self, m_post):
+        from pyramid_mixpanel import MixpanelUtility
+
+        transaction = {
+            '$time': '2013-01-24T14:30:00+0000',
+            '$amount': 25.00
+        }
+
+        mu = MixpanelUtility({u'mixpanel.token': u'token'})
+
+        mu.people_track(
+            u'some ID', {u'$transaction': transaction}, action='$append')
+
+        data = base64.b64encode(json.dumps({
+            u'$token': u'token',
+            u'$distinct_id': u'some ID',
+            u'$append': {
+                '$transaction': transaction,
+            },
+        }))
+        m_post.assert_called_once_with('https://api.mixpanel.com/engage/',
+                                       {'data': data})
+
+    @mock.patch('requests.post')
     @mock.patch('pyramid_mixpanel.log')
     def test_people_track_exception(self, m_log, m_post):
         from pyramid_mixpanel import MixpanelUtility