thinkingdata-ruby 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: af3bee9acd623d7588b142a62e6ecef998c359c5
4
- data.tar.gz: d9f7170bfc7dc600a58fcafbbfba45f92049f6d0
2
+ SHA256:
3
+ metadata.gz: 3885c858dfa78a2f34e1803351c1f3b20ed9f67378d7cf3033c0e840e3f9c60a
4
+ data.tar.gz: c0c954d2974d3000f708dd2ff6340a0799dd8a5c0aac84832c9b7fd245550381
5
5
  SHA512:
6
- metadata.gz: 6adee954d6aee35f30ed2a97074ce00500a71b4c02f3b4c1dded0ca44bcfc0e1636a0a564b49b4640114eae5d5a34c978630a8247d59bc6d0547f47deacd2ba6
7
- data.tar.gz: d9052f017aae1f57252b7a15abd921f4e23d026c6010bba6d868b39f638a63f782a6a47c1f2faaa46352da11372aae62769601c65e53cde3895ec8d12cbd3be6
6
+ metadata.gz: 9505c957f69a7270ed77b4b8a5a84828c5976aefebcea9635b4a4563182c134b102c75a7c2a3bc0ff55131465d3b588a3a878c9587a371303f61ae2ab0f257d8
7
+ data.tar.gz: 2bb4637c114d94bdf36f210588226d7bb8c7306377f6500d0b3b530f92e22ef7c3776b0ee48230bb4fc1e9bfe4bccfcd2e19c4b73c7861d1bd275d3a37e02f02
@@ -1,3 +1,7 @@
1
+ **1.2.0** (2020/08/28)
2
+ - 新增track_update接口,支持可更新事件
3
+ - 新增track_overwrite接口,支持可重写事件
4
+
1
5
  **v1.1.0** (2020/02/11)
2
6
  - 数据类型支持array类型
3
7
  - 新增 user_append 接口,支持用户的数组类型的属性追加
@@ -6,9 +6,10 @@ module TDAnalytics
6
6
  # 有数据时,首先会加入本地缓冲区,当条数到达上限后会发起上报
7
7
  class BatchConsumer
8
8
  # 默认缓冲区大小
9
- MAX_LENGTH = 20
9
+ DEFAULT_LENGTH = 20
10
+ MAX_LENGTH = 2000
10
11
 
11
- def initialize(server_url, app_id, max_buffer_length = MAX_LENGTH)
12
+ def initialize(server_url, app_id, max_buffer_length = DEFAULT_LENGTH)
12
13
  @server_uri = URI.parse(server_url)
13
14
  @server_uri.path = '/sync_server'
14
15
  @app_id = app_id
@@ -43,7 +44,13 @@ module TDAnalytics
43
44
  data = chunk.to_json
44
45
  end
45
46
  compress_type = @compress ? 'gzip' : 'none'
46
- headers = {'Content-Type' => 'application/plaintext', 'appid' => @app_id, 'compress' => compress_type}
47
+ headers = {'Content-Type' => 'application/plaintext',
48
+ 'appid' => @app_id,
49
+ 'compress' => compress_type,
50
+ 'TA-Integration-Type'=>'Ruby',
51
+ 'TA-Integration-Version'=>TDAnalytics::VERSION,
52
+ 'TA-Integration-Count'=>@buffers.count,
53
+ 'TA_Integration-Extra'=>'batch'}
47
54
  request = CaseSensitivePost.new(@server_uri.request_uri, headers)
48
55
  request.body = data
49
56
 
@@ -15,9 +15,15 @@ module TDAnalytics
15
15
 
16
16
  def add(message)
17
17
  puts message.to_json
18
+ headers = {
19
+ 'TA-Integration-Type'=>'Ruby',
20
+ 'TA-Integration-Version'=>TDAnalytics::VERSION,
21
+ 'TA-Integration-Count'=>'1',
22
+ 'TA_Integration-Extra'=>'debug'
23
+ }
18
24
  form_data = {"data" => message.to_json, "appid" => @app_id, "dryRun" => @write_data ? "0" : "1", "source" => "server"}
19
25
  begin
20
- response_code, response_body = request(@server_uri, form_data)
26
+ response_code, response_body = request(@server_uri, form_data,headers)
21
27
  rescue => e
22
28
  raise ConnectionError.new("Could not connect to TA server, with error \"#{e.message}\".")
23
29
  end
@@ -36,8 +42,8 @@ module TDAnalytics
36
42
  end
37
43
  end
38
44
 
39
- def request(uri, form_data)
40
- request = Net::HTTP::Post.new(uri.request_uri)
45
+ def request(uri, form_data,headers)
46
+ request = Net::HTTP::Post.new(uri.request_uri,headers)
41
47
  request.set_form_data(form_data)
42
48
 
43
49
  client = Net::HTTP.new(uri.host, uri.port)
@@ -67,7 +67,7 @@ module TDAnalytics
67
67
  # time: (可选)Time 事件发生时间,如果不传默认为系统当前时间
68
68
  # ip: (可选) 事件 IP,如果传入 IP 地址,后端可以通过 IP 地址解析事件发生地点
69
69
  # skip_local_check: (可选) boolean 表示是否跳过本地检测
70
- def track(event_name: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, skip_local_check: false)
70
+ def track(event_name: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil,first_check_id:nil, skip_local_check: false)
71
71
  begin
72
72
  _check_name event_name
73
73
  _check_id(distinct_id, account_id)
@@ -85,11 +85,80 @@ module TDAnalytics
85
85
  data[:account_id] = account_id if account_id
86
86
  data[:time] = time if time
87
87
  data[:ip] = ip if ip
88
+ data[:first_check_id] = first_check_id if first_check_id
88
89
  data[:properties] = properties
89
90
 
90
91
  _internal_track(:track, data)
91
92
  end
92
93
 
94
+ # 上报事件数据可进行更新. 每个事件都包含一个事件名和事件ID以及 Hash 对象的时间属性. 其参数说明如下:
95
+ # event_name: (必须) 事件名 必须是英文字母开头,可以包含字母、数字和 _, 长度不超过 50 个字符.
96
+ # event_id:(必须) event_name + event_id 会作为一条事件的唯一键
97
+ # distinct_id: (可选) 访客 ID
98
+ # account_id: (可选) 账号ID distinct_id 和 account_id 不能同时为空
99
+ # properties: (可选) Hash 事件属性。支持四种类型的值:字符串、数值、Time、boolean
100
+ # time: (可选)Time 事件发生时间,如果不传默认为系统当前时间
101
+ # ip: (可选) 事件 IP,如果传入 IP 地址,后端可以通过 IP 地址解析事件发生地点
102
+ # skip_local_check: (可选) boolean 表示是否跳过本地检测
103
+ def track_overwrite(event_name: nil,event_id: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, skip_local_check: false)
104
+ begin
105
+ _check_name event_name
106
+ _check_event_id event_id
107
+ _check_id(distinct_id, account_id)
108
+ unless skip_local_check
109
+ _check_properties(:track_overwrite, properties)
110
+ end
111
+ rescue TDAnalyticsError => e
112
+ @error_handler.handle(e)
113
+ return false
114
+ end
115
+
116
+ data = {}
117
+ data[:event_name] = event_name
118
+ data[:event_id] = event_id
119
+ data[:distinct_id] = distinct_id if distinct_id
120
+ data[:account_id] = account_id if account_id
121
+ data[:time] = time if time
122
+ data[:ip] = ip if ip
123
+ data[:properties] = properties
124
+ _internal_track(:track_overwrite, data)
125
+ end
126
+
127
+
128
+
129
+ # 上报事件数据可进行覆盖. 每个事件都包含一个事件名和事件ID以及 Hash 对象的时间属性. 其参数说明如下:
130
+ # event_name: (必须) 事件名 必须是英文字母开头,可以包含字母、数字和 _, 长度不超过 50 个字符.
131
+ # event_id:(必须) event_name + event_id 会作为一条事件的唯一键
132
+ # distinct_id: (可选) 访客 ID
133
+ # account_id: (可选) 账号ID distinct_id 和 account_id 不能同时为空
134
+ # properties: (可选) Hash 事件属性。支持四种类型的值:字符串、数值、Time、boolean
135
+ # time: (可选)Time 事件发生时间,如果不传默认为系统当前时间
136
+ # ip: (可选) 事件 IP,如果传入 IP 地址,后端可以通过 IP 地址解析事件发生地点
137
+ # skip_local_check: (可选) boolean 表示是否跳过本地检测
138
+ def track_update(event_name: nil,event_id: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, skip_local_check: false)
139
+ begin
140
+ _check_name event_name
141
+ _check_event_id event_id
142
+ _check_id(distinct_id, account_id)
143
+ unless skip_local_check
144
+ _check_properties(:track_update, properties)
145
+ end
146
+ rescue TDAnalyticsError => e
147
+ @error_handler.handle(e)
148
+ return false
149
+ end
150
+
151
+ data = {}
152
+ data[:event_name] = event_name
153
+ data[:event_id] = event_id
154
+ data[:distinct_id] = distinct_id if distinct_id
155
+ data[:account_id] = account_id if account_id
156
+ data[:time] = time if time
157
+ data[:ip] = ip if ip
158
+ data[:properties] = properties
159
+ _internal_track(:track_update, data)
160
+ end
161
+
93
162
  # 设置用户属性. 如果出现同名属性,则会覆盖之前的值.
94
163
  # distinct_id: (可选) 访客 ID
95
164
  # account_id: (可选) 账号ID distinct_id 和 account_id 不能同时为空
@@ -236,13 +305,13 @@ module TDAnalytics
236
305
  private
237
306
 
238
307
  # 出现异常的时候返回 false, 否则 true
239
- def _internal_track(type, properties: {}, event_name: nil, account_id: nil, distinct_id: nil, ip: nil, time: Time.now)
308
+ def _internal_track(type, properties: {}, event_name: nil, event_id:nil, account_id: nil, distinct_id: nil, ip: nil,first_check_id: nil, time: Time.now)
240
309
  if account_id == nil && distinct_id == nil
241
310
  raise IllegalParameterError.new('account id or distinct id must be provided.')
242
311
  end
243
312
 
244
- if type == :track
245
- raise IllegalParameterError.new('event name is empty for track') if event_name == nil
313
+ if type == :track || type == :track_update || type == :track_overwrite
314
+ raise IllegalParameterError.new('event name is empty') if event_name == nil
246
315
  properties = {'#zone_offset': time.utc_offset / 3600.0}.merge(LIB_PROPERTIES).merge(@super_properties).merge(properties)
247
316
  end
248
317
 
@@ -259,10 +328,12 @@ module TDAnalytics
259
328
  'properties' => properties,
260
329
  }
261
330
 
262
- data['#event_name'] = event_name if type == :track
331
+ data['#event_name'] = event_name if (type == :track || type == :track_update || :track_overwrite)
332
+ data['#event_id'] = event_id if (type == :track_update || type == :track_overwrite)
263
333
  data['#account_id'] = account_id if account_id
264
334
  data['#distinct_id'] = distinct_id if distinct_id
265
335
  data['#ip'] = ip if ip
336
+ data['#first_check_id'] = first_check_id if first_check_id
266
337
  data['#uuid'] = SecureRandom.uuid if @uuid
267
338
 
268
339
  ret = true
@@ -281,6 +352,11 @@ module TDAnalytics
281
352
  time.strftime("%Y-%m-%d %H:%M:%S.#{((time.to_f * 1000.0).to_i % 1000).to_s.rjust(3, "0")}")
282
353
  end
283
354
 
355
+ def _check_event_id(event_id)
356
+ raise IllegalParameterError.new("the event_id or property cannot be nil") if event_id.nil?
357
+ true
358
+ end
359
+
284
360
  # 属性名或者事件名检查
285
361
  def _check_name(name)
286
362
  raise IllegalParameterError.new("the name of event or property cannot be nil") if name.nil?
@@ -288,10 +364,6 @@ module TDAnalytics
288
364
  unless name.instance_of?(String) || name.instance_of?(Symbol)
289
365
  raise IllegalParameterError.new("#{name} is invalid. It must be String or Symbol")
290
366
  end
291
-
292
- unless name =~ /^[a-zA-Z][a-zA-Z0-9_]{1,49}$/
293
- raise IllegalParameterError.new("#{name} is invalid. It must be string starts with letters and contains letters, numbers, and _ with max length of 50")
294
- end
295
367
  true
296
368
  end
297
369
 
@@ -303,6 +375,7 @@ module TDAnalytics
303
375
 
304
376
  properties.each do |k, v|
305
377
  _check_name k
378
+ next if v.nil?
306
379
  unless v.is_a?(Integer) || v.is_a?(Float) || v.is_a?(Symbol) || v.is_a?(String) || v.is_a?(Time) || !!v == v || v.is_a?(Array)
307
380
  raise IllegalParameterError.new("The value of properties must be type in Integer, Float, Symbol, String, Array,and Time")
308
381
  end
@@ -324,14 +397,6 @@ module TDAnalytics
324
397
  # 检查用户 ID 合法性
325
398
  def _check_id(distinct_id, account_id)
326
399
  raise IllegalParameterError.new("account id or distinct id must be provided.") if distinct_id.nil? && account_id.nil?
327
-
328
- unless distinct_id.nil?
329
- raise IllegalParameterError.new("The length of distinct id should in (0, 64]") if distinct_id.to_s.length < 1 || distinct_id.to_s.length > 64
330
- end
331
-
332
- unless account_id.nil?
333
- raise IllegalParameterError.new("The length of account id should in (0, 64]") if account_id.to_s.length < 1 || account_id.to_s.length > 64
334
- end
335
400
  end
336
401
  end
337
402
  end
@@ -1,3 +1,3 @@
1
1
  module TDAnalytics
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thinkingdata-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ThinkingData
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-11 00:00:00.000000000 Z
11
+ date: 2020-08-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: The official ThinkingData Analytics API for ruby
14
14
  email: sdk@thinkingdata.cn
@@ -48,8 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubyforge_project:
52
- rubygems_version: 2.5.2.3
51
+ rubygems_version: 3.0.3
53
52
  signing_key:
54
53
  specification_version: 4
55
54
  summary: Official ThinkingData Analytics API for ruby