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
# 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
  TDLog.info("SDK init success.")
end

Instance Method Details

#clear_dynamic_super_propertiesObject

Clear dynamic super properties



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

def clear_dynamic_super_properties
  @dynamic_block = nil
end

#clear_super_propertiesObject

Clear super properties



86
87
88
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 86

def clear_super_properties
  @super_properties = {}
end

#closeObject

Close and exit sdk



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 348

def close
  return true unless defined? @consumer.close
  ret = true
  begin
    @consumer.close
  rescue TDAnalyticsError => e
    @error_handler.handle(e)
    ret = false
  end

  TDLog.info("SDK close.")

  ret
end

#flushObject

Report data immediately



333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 333

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



92
93
94
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 92

def set_dynamic_super_properties(&block)
  @dynamic_block = block
end

#set_super_properties(properties, skip_local_check = false) ⇒ Object

Set common properties



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

def set_super_properties(properties, skip_local_check = false)
  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

#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)  ID. distinct_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


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 112

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)  ID. distinct_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


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 137

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)  ID. distinct_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


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 163

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)  ID. distinct_id,  can't both be empty.
properties: (optional) string、number、Time、boolean


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 297

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)  ID. distinct_id,  can't both be empty.
properties: (optional) string、number、Time、boolean


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 225

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)  ID. distinct_id,  can't both be empty.


317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 317

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)  ID. distinct_id,  can't both be empty.
properties: (optional) string、number、Time、boolean
ip: (optional) ip


185
186
187
188
189
190
191
192
193
194
195
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 185

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)  ID. distinct_id,  can't both be empty.
properties: (optional) string、number、Time、boolean
ip: (optional) ip


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 203

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)  ID. distinct_id,  can't both be empty.
properties: (optional) string、number、Time、boolean


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 246

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)  ID. distinct_id,  can't both be empty.
properties: (optional) string、number、Time、boolean


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 267

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