Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5dfb5b

Browse files
author
Fu Lili
committed
新增 is_login_id 参数
1 parent 1acfce8 commit b5dfb5b

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

sensorsanalytics/sdk.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import urllib2
2525
import urllib
2626

27-
SDK_VERSION = '1.5.3'
27+
SDK_VERSION = '1.7.0'
2828

2929
try:
3030
isinstance("", basestring)
@@ -90,7 +90,7 @@ def default(self, obj):
9090
return obj.strftime(fmt)
9191
return json.JSONEncoder.default(self, obj)
9292

93-
def __init__(self, consumer=None, project_name=None, enable_time_free = False):
93+
def __init__(self, consumer=None, project_name=None, enable_time_free=False):
9494
"""
9595
初始化一个 SensorsAnalytics 的实例。可以选择使用默认的 DefaultConsumer,也可以选择其它的 Consumer 实现。
9696
@@ -135,7 +135,7 @@ def clear_super_properties(self):
135135
'$lib_version': SDK_VERSION,
136136
}
137137

138-
def track(self, distinct_id, event_name, properties=None):
138+
def track(self, distinct_id, event_name, properties=None, is_login_id=False):
139139
"""
140140
跟踪一个用户的行为。
141141
@@ -146,7 +146,7 @@ def track(self, distinct_id, event_name, properties=None):
146146
all_properties = self._super_properties.copy()
147147
if properties:
148148
all_properties.update(properties)
149-
self._track_event('track', event_name, distinct_id, None, all_properties)
149+
self._track_event('track', event_name, distinct_id, None, all_properties, is_login_id)
150150

151151
def track_signup(self, distinct_id, original_id, properties=None):
152152
"""
@@ -167,7 +167,7 @@ def track_signup(self, distinct_id, original_id, properties=None):
167167
if properties:
168168
all_properties.update(properties)
169169

170-
self._track_event('track_signup', '$SignUp', distinct_id, original_id, all_properties)
170+
self._track_event('track_signup', '$SignUp', distinct_id, original_id, all_properties, False)
171171

172172
@staticmethod
173173
def _normalize_data(data):
@@ -284,43 +284,43 @@ def _extract_user_time(properties):
284284
return t
285285
return None
286286

287-
def profile_set(self, distinct_id, profiles):
287+
def profile_set(self, distinct_id, profiles, is_login_id=False):
288288
"""
289289
直接设置一个用户的 Profile,如果已存在则覆盖
290290
291291
:param distinct_id: 用户的唯一标识
292292
:param profiles: 用户属性
293293
"""
294-
return self._track_event('profile_set', None, distinct_id, None, profiles)
294+
return self._track_event('profile_set', None, distinct_id, None, profiles, is_login_id)
295295

296-
def profile_set_once(self, distinct_id, profiles):
296+
def profile_set_once(self, distinct_id, profiles, is_login_id=False):
297297
"""
298298
直接设置一个用户的 Profile,如果某个 Profile 已存在则不设置。
299299
300300
:param distinct_id: 用户的唯一标识
301301
:param profiles: 用户属性
302302
"""
303-
return self._track_event('profile_set_once', None, distinct_id, None, profiles)
303+
return self._track_event('profile_set_once', None, distinct_id, None, profiles, is_login_id)
304304

305-
def profile_increment(self, distinct_id, profiles):
305+
def profile_increment(self, distinct_id, profiles, is_login_id=False):
306306
"""
307307
增减/减少一个用户的某一个或者多个数值类型的 Profile。
308308
309309
:param distinct_id: 用户的唯一标识
310310
:param profiles: 用户属性
311311
"""
312-
return self._track_event('profile_increment', None, distinct_id, None, profiles)
312+
return self._track_event('profile_increment', None, distinct_id, None, profiles, is_login_id)
313313

314-
def profile_append(self, distinct_id, profiles):
314+
def profile_append(self, distinct_id, profiles, is_login_id=False):
315315
"""
316316
追加一个用户的某一个或者多个集合类型的 Profile。
317317
318318
:param distinct_id: 用户的唯一标识
319319
:param profiles: 用户属性
320320
"""
321-
return self._track_event('profile_append', None, distinct_id, None, profiles)
321+
return self._track_event('profile_append', None, distinct_id, None, profiles, is_login_id)
322322

323-
def profile_unset(self, distinct_id, profile_keys):
323+
def profile_unset(self, distinct_id, profile_keys, is_login_id=False):
324324
"""
325325
删除一个用户的一个或者多个 Profile。
326326
@@ -329,17 +329,17 @@ def profile_unset(self, distinct_id, profile_keys):
329329
"""
330330
if isinstance(profile_keys, list):
331331
profile_keys = dict((key, True) for key in profile_keys)
332-
return self._track_event('profile_unset', None, distinct_id, None, profile_keys)
332+
return self._track_event('profile_unset', None, distinct_id, None, profile_keys, is_login_id)
333333

334-
def profile_delete(self, distinct_id):
334+
def profile_delete(self, distinct_id, is_login_id=False):
335335
"""
336336
删除整个用户的信息。
337337
338338
:param distinct_id: 用户的唯一标识
339339
"""
340-
return self._track_event('profile_delete', None, distinct_id, None, {})
340+
return self._track_event('profile_delete', None, distinct_id, None, {}, is_login_id)
341341

342-
def _track_event(self, event_type, event_name, distinct_id, original_id, properties):
342+
def _track_event(self, event_type, event_name, distinct_id, original_id, properties, is_login_id):
343343
event_time = self._extract_user_time(properties) or self._now()
344344

345345
data = {
@@ -361,6 +361,9 @@ def _track_event(self, event_type, event_name, distinct_id, original_id, propert
361361
if self._enable_time_free:
362362
data["time_free"] = True
363363

364+
if is_login_id:
365+
properties["$is_login_id"] = True
366+
364367
data = self._normalize_data(data)
365368
self._consumer.send(self._json_dumps(data))
366369

sensorsanalytics/test_sdk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from sdk import *
77

88

9-
TEST_URL_PREFIX = 'http://git.sensorsdata.cn/test'
10-
TEST_DEBUG_URL_PREFIX = 'http://10.10.229.134:8001/debug'
9+
TEST_URL_PREFIX = 'http://10.10.11.209:8006/sa?token=bbb'
10+
TEST_DEBUG_URL_PREFIX = 'http://10.10.11.209:8006/debug?token=bbb'
1111

1212

1313
class NormalTest(unittest.TestCase):
@@ -53,7 +53,7 @@ def clear_msg_counter(self):
5353
def testDebug(self):
5454
consumer = DebugConsumer(TEST_DEBUG_URL_PREFIX, False)
5555
sa = SensorsAnalytics(consumer)
56-
sa.track(1234, 'Test', {'From': 'Baidu'})
56+
sa.track(1234, 'Test', {'From': 'Baidu'}, is_login_id=True)
5757
consumer = DebugConsumer(TEST_DEBUG_URL_PREFIX, True)
5858
sa = SensorsAnalytics(consumer)
5959
sa.track(1234, 'Test', {'From': 456})

0 commit comments

Comments
 (0)