Class: Segment::Analytics::Client
- Inherits:
-
Object
- Object
- Segment::Analytics::Client
- Defined in:
- lib/segment/analytics/client.rb
Constant Summary
Constants included from Utils
Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON
Instance Method Summary collapse
-
#alias(attrs) ⇒ Object
Aliases a user from one id to another.
-
#flush ⇒ Object
Synchronously waits until the worker has flushed the queue.
-
#group(attrs) ⇒ Object
Associates a user identity with a group.
-
#identify(attrs) ⇒ Object
Identifies a user.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#page(attrs) ⇒ Object
Records a page view.
-
#queued_messages ⇒ Fixnum
Number of messages in the queue.
-
#screen(attrs) ⇒ Object
Records a screen view (for a mobile app).
-
#track(attrs) ⇒ Object
Tracks an event.
Methods included from Logging
Methods included from Utils
#date_in_iso8601, #datetime_in_iso8601, #formatted_offset, #isoify_dates, #isoify_dates!, #seconds_to_utc_offset, #stringify_keys, #symbolize_keys, #symbolize_keys!, #time_in_iso8601, #uid
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/segment/analytics/client.rb', line 20 def initialize(opts = {}) symbolize_keys!(opts) @queue = Queue.new @write_key = opts[:write_key] @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE @options = opts @worker_mutex = Mutex.new @worker = Worker.new(@queue, @write_key, @options) check_write_key! at_exit { @worker_thread && @worker_thread[:should_exit] = true } end |
Instance Method Details
#alias(attrs) ⇒ Object
Aliases a user from one id to another
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/segment/analytics/client.rb', line 161 def alias(attrs) symbolize_keys! attrs from = attrs[:previous_id] to = attrs[:user_id] = attrs[:timestamp] || Time.new context = attrs[:context] || {} = attrs[:message_id].to_s if attrs[:message_id] check_presence! from, 'previous_id' check_presence! to, 'user_id' add_context context enqueue({ :previousId => from, :userId => to, :integrations => attrs[:integrations], :context => context, :options => attrs[:options], :messageId => , :timestamp => datetime_in_iso8601(), :type => 'alias' }) end |
#flush ⇒ Object
Synchronously waits until the worker has flushed the queue.
Use only for scripts which are not long-running, and will specifically exit
39 40 41 42 43 44 |
# File 'lib/segment/analytics/client.rb', line 39 def flush while !@queue.empty? || @worker.is_requesting? ensure_worker_running sleep(0.1) end end |
#group(attrs) ⇒ Object
Associates a user identity with a group.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/segment/analytics/client.rb', line 205 def group(attrs) symbolize_keys! attrs check_user_id! attrs group_id = attrs[:group_id] user_id = attrs[:user_id] traits = attrs[:traits] || {} = attrs[:timestamp] || Time.new context = attrs[:context] || {} = attrs[:message_id].to_s if attrs[:message_id] raise ArgumentError, '.traits must be a hash' unless traits.is_a? Hash isoify_dates! traits check_presence! group_id, 'group_id' add_context context enqueue({ :groupId => group_id, :userId => user_id, :traits => traits, :integrations => attrs[:integrations], :options => attrs[:options], :context => context, :messageId => , :timestamp => datetime_in_iso8601(), :type => 'group' }) end |
#identify(attrs) ⇒ Object
Identifies a user
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/segment/analytics/client.rb', line 118 def identify(attrs) symbolize_keys! attrs check_user_id! attrs traits = attrs[:traits] || {} = attrs[:timestamp] || Time.new context = attrs[:context] || {} = attrs[:message_id].to_s if attrs[:message_id] raise ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash isoify_dates! traits add_context context enqueue({ :userId => attrs[:user_id], :anonymousId => attrs[:anonymous_id], :integrations => attrs[:integrations], :context => context, :traits => traits, :options => attrs[:options], :messageId => , :timestamp => datetime_in_iso8601(), :type => 'identify' }) end |
#page(attrs) ⇒ Object
Records a page view
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/segment/analytics/client.rb', line 255 def page(attrs) symbolize_keys! attrs check_user_id! attrs name = attrs[:name].to_s properties = attrs[:properties] || {} = attrs[:timestamp] || Time.new context = attrs[:context] || {} = attrs[:message_id].to_s if attrs[:message_id] raise ArgumentError, '.properties must be a hash' unless properties.is_a? Hash isoify_dates! properties add_context context enqueue({ :userId => attrs[:user_id], :anonymousId => attrs[:anonymous_id], :name => name, :category => attrs[:category], :properties => properties, :integrations => attrs[:integrations], :options => attrs[:options], :context => context, :messageId => , :timestamp => datetime_in_iso8601(), :type => 'page' }) end |
#queued_messages ⇒ Fixnum
Returns number of messages in the queue.
335 336 337 |
# File 'lib/segment/analytics/client.rb', line 335 def @queue.length end |
#screen(attrs) ⇒ Object
Records a screen view (for a mobile app)
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/segment/analytics/client.rb', line 303 def screen(attrs) symbolize_keys! attrs check_user_id! attrs name = attrs[:name].to_s properties = attrs[:properties] || {} = attrs[:timestamp] || Time.new context = attrs[:context] || {} = attrs[:message_id].to_s if attrs[:message_id] raise ArgumentError, '.properties must be a hash' unless properties.is_a? Hash isoify_dates! properties add_context context enqueue({ :userId => attrs[:user_id], :anonymousId => attrs[:anonymous_id], :name => name, :properties => properties, :category => attrs[:category], :options => attrs[:options], :integrations => attrs[:integrations], :context => context, :messageId => , :timestamp => .iso8601, :type => 'screen' }) end |
#track(attrs) ⇒ Object
Tracks an event
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/segment/analytics/client.rb', line 65 def track(attrs) symbolize_keys! attrs check_user_id! attrs event = attrs[:event] properties = attrs[:properties] || {} = attrs[:timestamp] || Time.new context = attrs[:context] || {} = attrs[:message_id].to_s if attrs[:message_id] if event.nil? || event.empty? raise ArgumentError, 'Must supply event as a non-empty string' end raise ArgumentError, 'Properties must be a Hash' unless properties.is_a? Hash isoify_dates! properties add_context context enqueue({ :event => event, :userId => attrs[:user_id], :anonymousId => attrs[:anonymous_id], :context => context, :options => attrs[:options], :integrations => attrs[:integrations], :properties => properties, :messageId => , :timestamp => datetime_in_iso8601(), :type => 'track' }) end |