Class: ThinkingData::TDAnalytics
- Inherits:
-
Object
- Object
- ThinkingData::TDAnalytics
- 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, }.freeze
Instance Method Summary collapse
-
#clear_dynamic_super_properties ⇒ Object
Clear dynamic super properties.
-
#clear_super_properties ⇒ Object
Clear super properties.
-
#close ⇒ Object
Close and exit sdk.
-
#flush ⇒ Object
Report data immediately.
-
#initialize(consumer, error_handler = nil, uuid: false) ⇒ TDAnalytics
constructor
Init function @param consumer [consumer] data consumer: TDLoggerConsumer | TDDebugConsumer | TDBatchConsumer @param error_handler [TDErrorHandler] custom error handler, process SDK error.
-
#set_dynamic_super_properties(&block) ⇒ Object
Set dynamic super properties.
-
#set_super_properties(properties, skip_local_check = false) ⇒ Object
Set common properties.
-
#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.
-
#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.
-
#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.
-
#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.
-
#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.
-
#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.
-
#user_set(distinct_id: nil, account_id: nil, properties: {}, ip: nil) ⇒ Object
Set user properties.
-
#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.
-
#user_uniq_append(distinct_id: nil, account_id: nil, properties: {}) ⇒ Object
To append user properties of array type.
-
#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.
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
58 59 60 61 62 63 64 65 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 58 def initialize(consumer, error_handler = nil, uuid: false) @error_handler = error_handler || TDErrorHandler.new @consumer = consumer @super_properties = {}.freeze @uuid_enable = uuid @mutex = Mutex.new TDLog.info("SDK init success.") end |
Instance Method Details
#clear_dynamic_super_properties ⇒ Object
Clear dynamic super properties
101 102 103 104 105 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 101 def clear_dynamic_super_properties @mutex.synchronize do @dynamic_block = nil end end |
#clear_super_properties ⇒ Object
Clear super properties
85 86 87 88 89 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 85 def clear_super_properties @mutex.synchronize do @super_properties = {}.freeze end end |
#close ⇒ Object
Close and exit sdk
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 353 def close return true unless @consumer.respond_to?(:close) ret = true # Consumer 自身已有锁保护,无需在此加锁 begin @consumer.close rescue TDAnalyticsError => e @error_handler.handle(e) ret = false end TDLog.info("SDK close.") ret end |
#flush ⇒ Object
Report data immediately
338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 338 def flush TDLog.info("SDK flush data.") return true unless @consumer.respond_to?(: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
93 94 95 96 97 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 93 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
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 69 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 formatted = {} properties.each do |k, v| formatted[k] = v.is_a?(Time) ? _format_time(v) : v end @super_properties = @super_properties.merge(formatted).freeze 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 117 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, account_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: 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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 142 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, account_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: 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 168 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, account_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: 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
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 302 def user_add(distinct_id: nil, account_id: nil, properties: {}) begin _check_id(distinct_id, account_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: 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
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 230 def user_append(distinct_id: nil, account_id: nil, properties: {}) begin _check_id(distinct_id, account_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: 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.
322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 322 def user_del(distinct_id: nil, account_id: nil) begin _check_id(distinct_id, account_id) rescue TDAnalyticsError => e @error_handler.handle(e) return false end _internal_track(:user_del, distinct_id: distinct_id, account_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
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 190 def user_set(distinct_id: nil, account_id: nil, properties: {}, ip: nil) begin _check_id(distinct_id, account_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: 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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 208 def user_set_once(distinct_id: nil, account_id: nil, properties: {}, ip: nil) begin _check_id(distinct_id, account_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: 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
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 251 def user_uniq_append(distinct_id: nil, account_id: nil, properties: {}) begin _check_id(distinct_id, account_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: 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
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/thinkingdata-ruby/td_analytics.rb', line 272 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, account_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: account_id, properties: properties, ) end |