Class: TDAnalytics::Tracker

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

Constant Summary collapse

LIB_PROPERTIES =
{
  '#lib' => 'ruby',
  '#lib_version' => TDAnalytics::VERSION,
}
@@dynamic_block =
nil

Instance Method Summary collapse

Constructor Details

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



39
40
41
42
43
44
# File 'lib/thinkingdata-ruby/tracker.rb', line 39

def initialize(consumer, error_handler = nil, uuid: false)
  @error_handler = error_handler || ErrorHandler.new
  @consumer = consumer
  @super_properties = {}
  @uuid = uuid
end

Instance Method Details

#clear_dynamic_super_propertiesObject



69
70
71
# File 'lib/thinkingdata-ruby/tracker.rb', line 69

def clear_dynamic_super_properties
  @@dynamic_block = nil
end

#clear_super_propertiesObject



61
62
63
# File 'lib/thinkingdata-ruby/tracker.rb', line 61

def clear_super_properties
  @super_properties = {}
end

#closeObject

Close and exit sdk



267
268
269
270
271
272
273
274
275
276
277
# File 'lib/thinkingdata-ruby/tracker.rb', line 267

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

#flushObject

report data immediately.



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/thinkingdata-ruby/tracker.rb', line 254

def flush
  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



65
66
67
# File 'lib/thinkingdata-ruby/tracker.rb', line 65

def set_dynamic_super_properties(&block)
  @@dynamic_block = block
end

#set_super_properties(properties, skip_local_check = false) ⇒ Object

set common properties



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/thinkingdata-ruby/tracker.rb', line 47

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


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/thinkingdata-ruby/tracker.rb', line 82

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/thinkingdata-ruby/tracker.rb', line 98

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/thinkingdata-ruby/tracker.rb', line 115

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/thinkingdata-ruby/tracker.rb', line 222

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 add user properties of array type.



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

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.



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/thinkingdata-ruby/tracker.rb', line 239

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.



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/thinkingdata-ruby/tracker.rb', line 132

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.



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

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/thinkingdata-ruby/tracker.rb', line 179

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.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/thinkingdata-ruby/tracker.rb', line 196

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