sensors_analytics_sdk 1.3.9 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sensors_analytics_sdk.rb +54 -17
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b47906b565b92fa888b74b24be9be5bd978fc7ef
4
- data.tar.gz: 3c4d01c3bd50aaa0ee1653d75a60b9a06d5b8284
3
+ metadata.gz: 1501e25d8b797a68d0d55f5ffaec58d1f6cd56aa
4
+ data.tar.gz: 9015ac1400a459f1c44be37f096c87392d91b44a
5
5
  SHA512:
6
- metadata.gz: 424d39177dcf78383f3a1d5a280195f045cd276deeafd77f3cb54a612663906ef9f20070874b8f4a2af24f6fc1801146b73428458167bba22b29ba0224bf00de
7
- data.tar.gz: 3eed9b537e9d5c26d846bfc8fc922573b0387d8797ce59d97e5f77698c356c5f9db0201d86fe3a841230bf5cab2dbc899c49fc398fb2e05f9662c871de6cfe0f
6
+ metadata.gz: 7ed4f21334dbcd623df8542bec482c774fee745e50eaf9834706055c62050a79a896a87b595c8a133604f5e7a39cc4d22ea448e9f840a55ce00c8aca33e3d0ff
7
+ data.tar.gz: a5bacf8b121e34843b47dc91b48687c6bf704050ae251e52fc0f0fd416b91fca618387d09b76bf17f0e358ad1e4daf319e1651374e7c26d24ad85bf9e6e897cd
@@ -5,7 +5,7 @@ require 'zlib'
5
5
 
6
6
  module SensorsAnalytics
7
7
 
8
- VERSION = '1.3.9'
8
+ VERSION = '1.4.0'
9
9
 
10
10
  KEY_PATTERN = /^((?!^distinct_id$|^original_id$|^time$|^properties$|^id$|^first_id$|^second_id$|^users$|^events$|^event$|^user_id$|^date$|^datetime$)[a-zA-Z_$][a-zA-Z\\d_$]{0,99})$/
11
11
 
@@ -60,9 +60,6 @@ module SensorsAnalytics
60
60
  #
61
61
  # 当 track 的 Properties 和 Super Properties 有相同的 key 时,将采用 track 的
62
62
  def register_super_properties(properties)
63
- # raise IllegalDataError
64
- _assert_properties(properties)
65
-
66
63
  properties.each do |key, value|
67
64
  @super_properties[key] = value
68
65
  end
@@ -144,7 +141,7 @@ module SensorsAnalytics
144
141
 
145
142
  # 从事件属性中获取时间配置
146
143
  event_time = _extract_time_from_properties(properties)
147
- properties.delete("$time")
144
+ properties.delete(:$time)
148
145
 
149
146
  event_properties = {}
150
147
  if event_type == :track || event_type == :track_signup
@@ -159,21 +156,24 @@ module SensorsAnalytics
159
156
  end
160
157
  end
161
158
 
159
+ lib_properties = _get_lib_properties()
160
+
162
161
  # Track / TrackSignup / ProfileSet / ProfileSetOne / ProfileIncrement / ProfileAppend / ProfileUnset
163
162
  event = {
164
- "type" => event_type,
165
- "time" => event_time,
166
- "distinct_id" => distinct_id,
167
- "properties" => event_properties,
163
+ :type => event_type,
164
+ :time => event_time,
165
+ :distinct_id => distinct_id,
166
+ :properties => event_properties,
167
+ :lib => lib_properties,
168
168
  }
169
169
 
170
170
  if event_type == :track
171
171
  # Track
172
- event["event"] = event_name
172
+ event[:event] = event_name
173
173
  elsif event_type == :track_signup
174
174
  # TrackSignUp
175
- event["event"] = event_name
176
- event["original_id"] = origin_distinct_id
175
+ event[:event] = event_name
176
+ event[:original_id] = origin_distinct_id
177
177
  end
178
178
 
179
179
  @consumer.send(event)
@@ -181,16 +181,42 @@ module SensorsAnalytics
181
181
 
182
182
  def _extract_time_from_properties(properties)
183
183
  properties.each do |key, value|
184
- if key == "$time" && value.is_a?(Time)
184
+ if (key == :$time || key == "$time") && value.is_a?(Time)
185
185
  return (value.to_f * 1000).to_i
186
186
  end
187
187
  end
188
188
  return (Time.now().to_f * 1000).to_i
189
189
  end
190
190
 
191
+ def _get_lib_properties()
192
+ lib_properties = {
193
+ '$lib' => 'Ruby',
194
+ '$lib_version' => VERSION,
195
+ '$lib_method' => 'code',
196
+ }
197
+
198
+ @super_properties.each do |key, value|
199
+ if key == :$app_version || key == "$app_version"
200
+ lib_properties[:$app_version] = value
201
+ end
202
+ end
203
+
204
+ begin
205
+ raise Exception
206
+ rescue Exception => e
207
+ trace = e.backtrace[3].split(':')
208
+ file = trace[0]
209
+ line = trace[1]
210
+ function = trace[2].split('`')[1][0..-2]
211
+ lib_properties[:$lib_detail] = "###{function}###{file}###{line}"
212
+ end
213
+
214
+ return lib_properties
215
+ end
216
+
191
217
  def _assert_key(type, key)
192
218
  unless key.instance_of?(String) || key.instance_of?(Symbol)
193
- raise IllegalDataError.new("#{type} must be an instance of String.")
219
+ raise IllegalDataError.new("#{type} must be an instance of String / Symbol.")
194
220
  end
195
221
  unless key.length >= 1
196
222
  raise IllegalDataError.new("#{type} is empty.")
@@ -286,7 +312,7 @@ module SensorsAnalytics
286
312
  init_header[key] = value
287
313
  end
288
314
 
289
- uri = URI(@server_url)
315
+ uri = _get_uri(@server_url)
290
316
  request = Net::HTTP::Post.new(uri.request_uri, initheader = init_header)
291
317
  request.set_form_data(form_data)
292
318
 
@@ -299,6 +325,17 @@ module SensorsAnalytics
299
325
  return [response.code, response.body]
300
326
  end
301
327
 
328
+ def _get_uri(url)
329
+ begin
330
+ URI.parse(url)
331
+ rescue URI::InvalidURIError
332
+ host = url.match(".+\:\/\/([^\/]+)")[1]
333
+ uri = URI.parse(url.sub(host, 'dummy-host'))
334
+ uri.instance_variable_set('@host', host)
335
+ uri
336
+ end
337
+ end
338
+
302
339
  end
303
340
 
304
341
  # 实现逐条、同步发送的 Consumer,初始化参数为 Sensors Analytics 收集数据的 URI
@@ -365,7 +402,7 @@ module SensorsAnalytics
365
402
  class DebugConsumer < SensorsAnalyticsConsumer
366
403
 
367
404
  def initialize(server_url, write_data)
368
- uri = URI(server_url)
405
+ uri = _get_uri(server_url)
369
406
  # 将 URL Path 替换成 Debug 模式的 '/debug'
370
407
  uri.path = '/debug'
371
408
 
@@ -397,7 +434,7 @@ module SensorsAnalytics
397
434
  raise DebugModeError.new("Could not write to Sensors Analytics, server responded with #{response_code} returning: '#{response_body}'")
398
435
  end
399
436
  end
400
-
437
+
401
438
  end
402
439
 
403
440
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensors_analytics_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.9
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuhan ZOU
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-16 00:00:00.000000000 Z
11
+ date: 2016-05-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This is the official Ruby SDK for Sensors Analytics.
14
14
  email: zouyuhan@sensorsdata.cn
@@ -36,7 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  version: '0'
37
37
  requirements: []
38
38
  rubyforge_project:
39
- rubygems_version: 2.0.14
39
+ rubygems_version: 2.0.14.1
40
40
  signing_key:
41
41
  specification_version: 4
42
42
  summary: SensorsAnalyticsSDK