Skip to content

Commit a646d1d

Browse files
author
Joe Bowers
committed
Merge branch 'master' of https://github.com/sbnoemi/mixpanel-python into 2.0.2rc
2 parents 80434af + a358427 commit a646d1d

2 files changed

Lines changed: 59 additions & 20 deletions

File tree

mixpanel/__init__.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, token, consumer=None):
3939
def _now(self):
4040
return time.time()
4141

42-
def track(self, distinct_id, event_name, properties={}):
42+
def track(self, distinct_id, event_name, properties={}, meta={}):
4343
"""
4444
Notes that an event has occurred, along with a distinct_id
4545
representing the source of that event (for example, a user id),
@@ -69,9 +69,10 @@ def track(self, distinct_id, event_name, properties={}):
6969
'event': event_name,
7070
'properties': all_properties,
7171
}
72+
event.update(meta)
7273
self._consumer.send('events', json.dumps(event))
7374

74-
def alias(self, alias_id, original):
75+
def alias(self, alias_id, original, meta={}):
7576
"""
7677
Gives custom alias to a people record.
7778
@@ -85,9 +86,9 @@ def alias(self, alias_id, original):
8586
'distinct_id': original,
8687
'alias': alias_id,
8788
'token': self._token,
88-
})
89+
}, meta=meta)
8990

90-
def people_set(self, distinct_id, properties):
91+
def people_set(self, distinct_id, properties, meta={}):
9192
"""
9293
Set properties of a people record.
9394
@@ -100,9 +101,9 @@ def people_set(self, distinct_id, properties):
100101
return self.people_update({
101102
'$distinct_id': distinct_id,
102103
'$set': properties,
103-
})
104+
}, meta=meta)
104105

105-
def people_set_once(self, distinct_id, properties):
106+
def people_set_once(self, distinct_id, properties, meta={}):
106107
"""
107108
Set immutable properties of a people record.
108109
@@ -115,9 +116,9 @@ def people_set_once(self, distinct_id, properties):
115116
return self.people_update({
116117
'$distinct_id': distinct_id,
117118
'$set_once': properties,
118-
})
119+
}, meta=meta)
119120

120-
def people_increment(self, distinct_id, properties):
121+
def people_increment(self, distinct_id, properties, meta={}):
121122
"""
122123
Increments/decrements numerical properties of people record.
123124
@@ -130,9 +131,9 @@ def people_increment(self, distinct_id, properties):
130131
return self.people_update({
131132
'$distinct_id': distinct_id,
132133
'$add': properties,
133-
})
134+
}, meta=meta)
134135

135-
def people_append(self, distinct_id, properties):
136+
def people_append(self, distinct_id, properties, meta={}):
136137
"""
137138
Appends to the list associated with a property.
138139
@@ -146,9 +147,9 @@ def people_append(self, distinct_id, properties):
146147
return self.people_update({
147148
'$distinct_id': distinct_id,
148149
'$append': properties,
149-
})
150+
}, meta=meta)
150151

151-
def people_union(self, distinct_id, properties):
152+
def people_union(self, distinct_id, properties, meta={}):
152153
"""
153154
Merges the values for a list associated with a property.
154155
@@ -161,9 +162,9 @@ def people_union(self, distinct_id, properties):
161162
return self.people_update({
162163
'$distinct_id': distinct_id,
163164
'$union': properties,
164-
})
165+
}, meta=meta)
165166

166-
def people_unset(self, distinct_id, properties):
167+
def people_unset(self, distinct_id, properties, meta={}):
167168
"""
168169
Removes properties from a profile.
169170
@@ -175,9 +176,9 @@ def people_unset(self, distinct_id, properties):
175176
return self.people_update({
176177
'$distinct_id': distinct_id,
177178
'$unset': properties,
178-
})
179+
}, meta=meta)
179180

180-
def people_delete(self, distinct_id):
181+
def people_delete(self, distinct_id, meta={}):
181182
"""
182183
Permanently deletes a profile.
183184
@@ -189,9 +190,9 @@ def people_delete(self, distinct_id):
189190
return self.people_update({
190191
'$distinct_id': distinct_id,
191192
'$delete': "",
192-
})
193+
}, meta=meta)
193194

194-
def people_track_charge(self, distinct_id, amount, properties={}):
195+
def people_track_charge(self, distinct_id, amount, properties={}, meta={}):
195196
"""
196197
Tracks a charge to a user.
197198
@@ -206,9 +207,9 @@ def people_track_charge(self, distinct_id, amount, properties={}):
206207
mp.track_charge('1234', 50, {'$time': "2013-04-01T09:02:00"})
207208
"""
208209
properties.update({'$amount': amount})
209-
return self.people_append(distinct_id, {'$transactions': properties})
210+
return self.people_append(distinct_id, {'$transactions': properties}, meta=meta)
210211

211-
def people_update(self, message):
212+
def people_update(self, message, meta={}):
212213
"""
213214
Send a generic update to Mixpanel people analytics.
214215
Caller is responsible for formatting the update message, as
@@ -224,6 +225,7 @@ def people_update(self, message):
224225
'$time': int(self._now() * 1000),
225226
}
226227
record.update(message)
228+
record.update(meta)
227229
self._consumer.send('people', json.dumps(record))
228230

229231
class MixpanelException(Exception):

tests.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ def test_track(self):
4242
}
4343
}
4444
)])
45+
46+
def test_track_meta(self):
47+
self.mp.track('ID', 'button press', {'size': 'big', 'color': 'blue'},
48+
meta={'$ip': 0, '$ignore_time': True,})
49+
self.assertEqual(self.consumer.log, [(
50+
'events', {
51+
'event': 'button press',
52+
'properties': {
53+
'token': self.TOKEN,
54+
'size': 'big',
55+
'color': 'blue',
56+
'distinct_id': 'ID',
57+
'time': int(self.mp._now()),
58+
'mp_lib': 'python',
59+
'$lib_version': mixpanel.VERSION,
60+
},
61+
'$ip': 0,
62+
'$ignore_time': True,
63+
}
64+
)])
4565

4666
def test_people_set(self):
4767
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'})
@@ -154,6 +174,23 @@ def test_alias(self):
154174
}
155175

156176
)])
177+
178+
def test_people_meta(self):
179+
self.mp.people_set('amq', {'birth month': 'october', 'favorite color': 'purple'},
180+
meta={'$ip': 0, '$ignore_time': True})
181+
self.assertEqual(self.consumer.log, [(
182+
'people', {
183+
'$time': int(self.mp._now() * 1000),
184+
'$token': self.TOKEN,
185+
'$distinct_id': 'amq',
186+
'$set': {
187+
'birth month': 'october',
188+
'favorite color': 'purple',
189+
},
190+
'$ip': 0,
191+
'$ignore_time': True,
192+
}
193+
)])
157194

158195
class ConsumerTestCase(unittest.TestCase):
159196
def setUp(self):

0 commit comments

Comments
 (0)