Class: ThinkingData::TDAnalytics

Inherits:
Object
  • Object
show all
Defined in:
lib/thinkingdata-ruby/td_analytics.rb

Overview

Analytics class。 Provides the function of tracking data

Constant Summary collapse

LIB_PROPERTIES =
{
  '#lib' => 'ruby',
  '#lib_version' => ThinkingData::VERSION,
}

Instance Method Summary collapse

Constructor Details

#initialize(consumer, error_handler = nil, uuid: false) ⇒ TDAnalytics

Init function

@param consumer [consumer] data consumer: TDLoggerConsumer | TDDebugConsumer | TDBatchConsumer
@param error_handler [TDErrorHandler] custom error handler, process SDK error. It could be nil
@param uuid [Boolean] Whether to automatically add uuid


60
61
62
63
64
65
66
67
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 60

def initialize(consumer, error_handler = nil, uuid: false)
  @error_handler = error_handler || TDErrorHandler.new
  @consumer = consumer
  @super_properties = {}
  @uuid_enable = uuid
  @mutex = Mutex.new
  TDLog.info("SDK init success.")
end

Instance Method Details

#clear_dynamic_super_propertiesObject

Clear dynamic super properties



105
106
107
108
109
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 105

def clear_dynamic_super_properties
  @mutex.synchronize do
    @dynamic_block = nil
  end
end

#clear_super_propertiesObject

Clear super properties



89
90
91
92
93
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 89

def clear_super_properties
  @mutex.synchronize do
    @super_properties = {}
  end
end

#closeObject

Close and exit sdk



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 357

def close
  return true unless defined? @consumer.close
  ret = true
  # Consumer 自身已有锁保护,无需在此加锁
  begin
    @consumer.close
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    ret = false
  end

  TDLog.info("SDK close.")

  ret
end

#flushObject

Report data immediately



342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 342

def flush
  TDLog.info("SDK flush data.")
  return true unless defined? @consumer.flush
  ret = true
  begin
    @consumer.flush
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    ret = false
  end
  ret
end

#set_dynamic_super_properties(&block) ⇒ Object

Set dynamic super properties



97
98
99
100
101
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 97

def set_dynamic_super_properties(&block)
  @mutex.synchronize do
    @dynamic_block = block
  end
end

#set_super_properties(properties, skip_local_check = false) ⇒ Object

Set common properties



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 71

def set_super_properties(properties, skip_local_check = false)
  @mutex.synchronize do
    unless ThinkingData::get_stringent == false || skip_local_check || _check_properties(:track, properties)
      @error_handler.handle(IllegalParameterError.new("Invalid super properties"))
      return false
    end
    properties.each do |k, v|
      if v.is_a?(Time)
        @super_properties[k] = _format_time(v)
      else
        @super_properties[k] = v
      end
    end
  end
end

#track(event_name: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, first_check_id: nil, skip_local_check: false) ⇒ Object

Report ordinary event

event_name: (require) A string of 50 letters and digits that starts with '#' or a letter
distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean
time: (optional)Time
ip: (optional) ip
first_check_id: (optional) The value cannot be null for the first event
skip_local_check: (optional) check data or not


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 121

def track(event_name: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil,first_check_id:nil, skip_local_check: false)
  begin
    _check_name event_name
    _check_id(distinct_id, )
    unless skip_local_check
      _check_properties(:track, properties)
    end
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:track, event_name: event_name, distinct_id: distinct_id, account_id: , properties: properties, time: time, ip: ip, first_check_id: first_check_id)
end

#track_overwrite(event_name: nil, event_id: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, skip_local_check: false) ⇒ Object

Report overridable event

event_name: (require) A string of 50 letters and digits that starts with '#' or a letter
event_id: (require) string
distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean
time: (optional)Time
ip: (optional) ip
skip_local_check: (optional) check data or not


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 146

def track_overwrite(event_name: nil,event_id: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, skip_local_check: false)
  begin
    _check_name event_name
    _check_event_id event_id
    _check_id(distinct_id, )
    unless skip_local_check
      _check_properties(:track_overwrite, properties)
    end
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:track_overwrite, event_name: event_name, event_id: event_id, distinct_id: distinct_id, account_id: , properties: properties, time: time, ip: ip)
end

#track_update(event_name: nil, event_id: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, skip_local_check: false) ⇒ Object

Report updatable event

event_name: (require) A string of 50 letters and digits that starts with '#' or a letter
event_id: (require) string
distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean
time: (optional)Time
ip: (optional) ip
skip_local_check: (optional) check data or not


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 172

def track_update(event_name: nil,event_id: nil, distinct_id: nil, account_id: nil, properties: {}, time: nil, ip: nil, skip_local_check: false)
  begin
    _check_name event_name
    _check_event_id event_id
    _check_id(distinct_id, )
    unless skip_local_check
      _check_properties(:track_update, properties)
    end
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:track_update, event_name: event_name, event_id: event_id, distinct_id: distinct_id, account_id: , properties: properties, time: time, ip: ip)
end

#user_add(distinct_id: nil, account_id: nil, properties: {}) ⇒ Object

To accumulate operations against the property

distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 306

def user_add(distinct_id: nil, account_id: nil, properties: {})
  begin
    _check_id(distinct_id, )
    _check_properties(:user_add, properties)
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:user_add,
                  distinct_id: distinct_id,
                  account_id: ,
                  properties: properties,
  )
end

#user_append(distinct_id: nil, account_id: nil, properties: {}) ⇒ Object

To append user properties of array type

distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 234

def user_append(distinct_id: nil, account_id: nil, properties: {})
  begin
    _check_id(distinct_id, )
    _check_properties(:user_append, properties)
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:user_append,
                  distinct_id: distinct_id,
                  account_id: ,
                  properties: properties,
                  )
end

#user_del(distinct_id: nil, account_id: nil) ⇒ Object

Delete a user, This operation cannot be undone

distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.


326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 326

def user_del(distinct_id: nil, account_id: nil)
  begin
    _check_id(distinct_id, )
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:user_del,
                  distinct_id: distinct_id,
                  account_id: ,
  )
end

#user_set(distinct_id: nil, account_id: nil, properties: {}, ip: nil) ⇒ Object

Set user properties. would overwrite existing names

distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean
ip: (optional) ip


194
195
196
197
198
199
200
201
202
203
204
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 194

def user_set(distinct_id: nil, account_id: nil, properties: {}, ip: nil)
  begin
    _check_id(distinct_id, )
    _check_properties(:user_set, properties)
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:user_set, distinct_id: distinct_id, account_id: , properties: properties, ip: ip)
end

#user_set_once(distinct_id: nil, account_id: nil, properties: {}, ip: nil) ⇒ Object

Set user properties, If such property had been set before, this message would be neglected

distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean
ip: (optional) ip


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 212

def user_set_once(distinct_id: nil, account_id: nil, properties: {}, ip: nil)
  begin
    _check_id(distinct_id, )
    _check_properties(:user_setOnce, properties)
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:user_setOnce,
                  distinct_id: distinct_id,
                  account_id: ,
                  properties: properties,
                  ip: ip,
  )
end

#user_uniq_append(distinct_id: nil, account_id: nil, properties: {}) ⇒ Object

To append user properties of array type. It filters out duplicate values

distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 255

def user_uniq_append(distinct_id: nil, account_id: nil, properties: {})
  begin
    _check_id(distinct_id, )
    _check_properties(:user_uniq_append, properties)
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:user_uniq_append,
                  distinct_id: distinct_id,
                  account_id: ,
                  properties: properties,
                  )
end

#user_unset(distinct_id: nil, account_id: nil, property: nil) ⇒ Object

Clear the user properties of users

distinct_id: (optional) distinct ID
account_id: (optional) account ID. distinct_id, account_id can't both be empty.
properties: (optional) string、number、Time、boolean


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 276

def user_unset(distinct_id: nil, account_id: nil, property: nil)
  properties = {}
  if property.is_a?(Array)
    property.each do |k|
      properties[k] = 0
    end
  else
    properties[property] = 0
  end

  begin
    _check_id(distinct_id, )
    _check_properties(:user_unset, properties)
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    return false
  end

  _internal_track(:user_unset,
                  distinct_id: distinct_id,
                  account_id: ,
                  properties: properties,
  )
end