Class: Segment::Analytics::Client
- Inherits:
-
Object
- Object
- Segment::Analytics::Client
- 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
-
#alias(options) ⇒ Object
public: Aliases a user from one id to another.
-
#flush ⇒ Object
public: Synchronously waits until the worker has flushed the queue.
-
#group(options) ⇒ Object
public: Associates a user identity with a group.
-
#identify(options) ⇒ Object
public: Identifies a user.
-
#initialize(options = {}) ⇒ Client
constructor
public: Creates a new client.
-
#page(options) ⇒ Object
public: Records a page view.
-
#queued_messages ⇒ Object
public: Returns the number of queued messages.
-
#screen(options) ⇒ Object
public: Records a screen view (for a mobile app).
-
#track(options) ⇒ Object
public: Tracks an event.
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 = {} symbolize_keys! @queue = Queue.new @write_key = [:write_key] @max_queue_size = [:max_queue_size] || Defaults::Queue::MAX_SIZE @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() symbolize_keys! from = [:previous_id] to = [:user_id] = [:timestamp] || Time.new context = [:context] || {} check_presence! from, 'previous_id' check_presence! to, 'user_id' add_context context enqueue({ :previousId => from, :userId => to, :integrations => [:integrations], :context => context, :timestamp => datetime_in_iso8601(), :type => 'alias' }) end |
#flush ⇒ Object
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() symbolize_keys! check_user_id! group_id = [:group_id] user_id = [:user_id] traits = [:traits] || {} = [:timestamp] || Time.new context = [:context] || {} fail 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 => [:integrations], :context => context, :timestamp => datetime_in_iso8601(), :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 symbolize_keys! check_user_id! traits = [:traits] || {} = [:timestamp] || Time.new context = [:context] || {} fail ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash isoify_dates! traits add_context context enqueue({ :userId => [:user_id], :anonymousId => [:anonymous_id], :integrations => [:integrations], :context => context, :traits => traits, :timestamp => datetime_in_iso8601(), :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() symbolize_keys! check_user_id! name = [:name].to_s properties = [:properties] || {} = [:timestamp] || Time.new context = [: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 add_context context enqueue({ :userId => [:user_id], :anonymousId => [:anonymous_id], :name => name, :category => [:category], :properties => properties, :integrations => [:integrations], :context => context, :timestamp => datetime_in_iso8601(), :type => 'page' }) end |
#queued_messages ⇒ Object
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 @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() symbolize_keys! check_user_id! name = [:name].to_s properties = [:properties] || {} = [:timestamp] || Time.new context = [: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 add_context context enqueue({ :userId => [:user_id], :anonymousId => [:anonymous_id], :name => name, :properties => properties, :category => [:category], :integrations => [:integrations], :context => context, :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 symbolize_keys! check_user_id! event = [:event] properties = [:properties] || {} = [:timestamp] || Time.new context = [:context] || {} 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 => [:user_id], :anonymousId => [:anonymous_id], :context => context, :integrations => [:integrations], :properties => properties, :timestamp => datetime_in_iso8601(), :type => 'track' }) end |