Class: Segment::Analytics::Client

Inherits:
Object
  • Object
show all
Includes:
Utils
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

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(options = {}) ⇒ Client

public: Creates a new client

options - Hash

:write_key         - String of your project's write_key
:max_queue_size - Fixnum of the max calls to remain queued (optional)
:on_error       - Proc which handles error calls from the API


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/segment/analytics/client.rb', line 18

def initialize options = {}
  symbolize_keys! options

  @queue = Queue.new
  @write_key = options[:write_key]
  @max_queue_size = options[:max_queue_size] || Defaults::Queue::MAX_SIZE
  @options = options
  @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(options) ⇒ Object

public: Aliases a user from one id to another

options - Hash

:previous_id      - String of the id to alias from
:user_id        - String of the id to alias to
:timestamp - Time of when the alias occured (optional)
:context   - Hash of context (optional)


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 124

def alias(options)
  symbolize_keys! options

  from = options[:previous_id]
  to = options[:user_id]
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  check_presence! from, 'previous_id'
  check_presence! to, 'user_id'
  check_timestamp! timestamp
  add_context context

  enqueue({
    :previousId => from,
    :userId => to,
    :integrations => options[:integrations],
    :context => context,
    :timestamp => datetime_in_iso8601(timestamp),
    :type => 'alias'
  })
end

#flushObject

public: Synchronously waits until the worker has flushed the queue.

Use only for scripts which are not long-running, and will
specifically exit


37
38
39
40
41
42
# File 'lib/segment/analytics/client.rb', line 37

def flush
  while !@queue.empty? || @worker.is_requesting?
    ensure_worker_running
    sleep(0.1)
  end
end

#group(options) ⇒ Object

public: Associates a user identity with a group.

options - Hash

:previous_id      - String of the id to alias from
:user_id        - String of the id to alias to
:timestamp - Time of when the alias occured (optional)
:context   - Hash of context (optional)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/segment/analytics/client.rb', line 154

def group(options)
  symbolize_keys! options
  check_user_id! options

  group_id = options[:group_id]
  user_id = options[:user_id]
  traits = options[:traits] || {}
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  fail ArgumentError, '.traits must be a hash' unless traits.is_a? Hash
  isoify_dates! traits

  check_presence! group_id, 'group_id'
  check_timestamp! timestamp
  add_context context

  enqueue({
    :groupId => group_id,
    :userId => user_id,
    :traits => traits,
    :integrations => options[:integrations],
    :context => context,
    :timestamp => datetime_in_iso8601(timestamp),
    :type => 'group'
  })
end

#identify(options) ⇒ Object

public: Identifies a user

options - Hash

:user_id   - String of the user id
:traits    - Hash of user traits. (optional)
:timestamp - Time of when the event occurred. (optional)
:context   - Hash of context. (optional)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/segment/analytics/client.rb', line 91

def identify options
  symbolize_keys! options
  check_user_id! options

  traits = options[:traits] || {}
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  check_timestamp! timestamp

  fail ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash
  isoify_dates! traits

  add_context context

  enqueue({
    :userId => options[:user_id],
    :anonymousId => options[:anonymous_id],
    :integrations => options[:integrations],
    :context => context,
    :traits => traits,
    :timestamp => datetime_in_iso8601(timestamp),
    :type => 'identify'
  })
end

#page(options) ⇒ Object

public: Records a page view

options - Hash

:user_id    - String of the id to alias from
:name       - String name of the page
:properties - Hash of page properties (optional)
:category   - String of the page category (optional)
:timestamp  - Time of when the pageview occured (optional)
:context    - Hash of context (optional)


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/segment/analytics/client.rb', line 191

def page(options)
  symbolize_keys! options
  check_user_id! options

  name = options[:name].to_s
  properties = options[:properties] || {}
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  fail ArgumentError, '.name must be a string' unless !name.empty?
  fail ArgumentError, '.properties must be a hash' unless properties.is_a? Hash
  isoify_dates! properties

  check_timestamp! timestamp
  add_context context

  enqueue({
    :userId => options[:user_id],
    :anonymousId => options[:anonymous_id],
    :name => name,
    :category => options[:category],
    :properties => properties,
    :integrations => options[:integrations],
    :context => context,
    :timestamp => datetime_in_iso8601(timestamp),
    :type => 'page'
  })
end

#queued_messagesObject

public: Returns the number of queued messages

returns Fixnum of messages in the queue



260
261
262
# File 'lib/segment/analytics/client.rb', line 260

def queued_messages
  @queue.length
end

#screen(options) ⇒ Object

public: Records a screen view (for a mobile app)

options - Hash

:user_id    - String of the id to alias from
:name       - String name of the screen
:category   - String screen category (optional)
:properties - Hash of screen properties (optional)
:timestamp  - Time of when the screen occured (optional)
:context    - Hash of context (optional)


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/segment/analytics/client.rb', line 228

def screen(options)
  symbolize_keys! options
  check_user_id! options

  name = options[:name].to_s
  properties = options[:properties] || {}
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  fail ArgumentError, '.name must be a string' if name.empty?
  fail ArgumentError, '.properties must be a hash' unless properties.is_a? Hash
  isoify_dates! properties

  check_timestamp! timestamp
  add_context context

  enqueue({
    :userId => options[:user_id],
    :anonymousId => options[:anonymous_id],
    :name => name,
    :properties => properties,
    :category => options[:category],
    :integrations => options[:integrations],
    :context => context,
    :timestamp => timestamp.iso8601,
    :type => 'screen'
  })
end

#track(options) ⇒ Object

public: Tracks an event

options - Hash

:event      - String of event name.
:user_id    - String of the user id.
:properties - Hash of event properties. (optional)
:timestamp  - Time of when the event occurred. (optional)
:context    - Hash of context. (optional)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/segment/analytics/client.rb', line 52

def track options
  symbolize_keys! options
  check_user_id! options

  event = options[:event]
  properties = options[:properties] || {}
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  check_timestamp! timestamp

  if event.nil? || event.empty?
    fail ArgumentError, 'Must supply event as a non-empty string'
  end

  fail ArgumentError, 'Properties must be a Hash' unless properties.is_a? Hash
  isoify_dates! properties

  add_context context

  enqueue({
    :event => event,
    :userId => options[:user_id],
    :anonymousId => options[:anonymous_id],
    :context =>  context,
    :integrations => options[:integrations],
    :properties => properties,
    :timestamp => datetime_in_iso8601(timestamp),
    :type => 'track'
  })
end