Class: AnalyticsRuby::Client
- Inherits:
-
Object
- Object
- AnalyticsRuby::Client
- Defined in:
- lib/analytics-ruby/client.rb
Defined Under Namespace
Classes: ConsumerThread
Instance Method Summary collapse
-
#alias(options) ⇒ Object
public: Aliases a user from one id to another.
-
#flush ⇒ Object
public: Synchronously waits until the consumer 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.
Constructor Details
#initialize(options = {}) ⇒ Client
public: Creates a new client
options - Hash
:secret - String of your project's secret
:max_queue_size - Fixnum of the max calls to remain queued (optional)
:on_error - Proc which handles error calls from the API
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/analytics-ruby/client.rb', line 23 def initialize( = {}) Util.symbolize_keys! @queue = Queue.new @secret = [:secret] @max_queue_size = [:max_queue_size] || Defaults::Queue::MAX_SIZE check_secret @consumer = Consumer.new @queue, @secret, @thread = ConsumerThread.new { @consumer.run } at_exit do # Let the consumer thread know it should exit. @thread[:should_exit] = true # Push a flag value to the consumer queue in case it's blocked waiting for a value. This will allow it # to continue its normal chain of processing, giving it a chance to exit. @queue << nil end end |
Instance Method Details
#alias(options) ⇒ Object
public: Aliases a user from one id to another
options - Hash
:from - String of the id to alias from
:to - String of the id to alias to
:timestamp - Time of when the alias occured (optional)
:context - Hash of context (optional)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/analytics-ruby/client.rb', line 140 def alias() check_secret Util.symbolize_keys! from = [:from].to_s to = [:to].to_s = [:timestamp] || Time.new context = [:context] || {} ensure_user from ensure_user to add_context context enqueue({ :from => from, :to => to, :context => context, :timestamp => .iso8601, :action => 'alias' }) end |
#flush ⇒ Object
public: Synchronously waits until the consumer has flushed the queue.
Use only for scripts which are not long-running, and will
specifically exit
50 51 52 53 54 |
# File 'lib/analytics-ruby/client.rb', line 50 def flush while !@queue.empty? || @consumer.is_requesting? sleep(0.1) end end |
#group(options) ⇒ Object
public: Associates a user identity with a group.
options - Hash
:from - String of the id to alias from
:to - String of the id to alias to
:timestamp - Time of when the alias occured (optional)
:context - Hash of context (optional)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/analytics-ruby/client.rb', line 169 def group() check_secret Util.symbolize_keys! group_id = [:group_id].to_s user_id = [:user_id].to_s traits = [:traits] || {} = [:timestamp] || Time.new context = [:context] || {} fail ArgumentError, '.traits must be a hash' unless traits.is_a? Hash ensure_user group_id ensure_user user_id add_context context enqueue({ :groupId => group_id, :userId => user_id, :traits => traits, :context => context, :timestamp => .iso8601, :action => '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)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/analytics-ruby/client.rb', line 105 def identify() check_secret Util.symbolize_keys! user_id = [:user_id].to_s traits = [:traits] || {} = [:timestamp] || Time.new context = [:context] || {} ensure_user user_id fail ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash Util.isoify_dates! traits add_context context enqueue({ :userId => user_id, :context => context, :traits => traits, :timestamp => .iso8601, :action => '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)
:timestamp - Time of when the pageview occured (optional)
:context - Hash of context (optional)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/analytics-ruby/client.rb', line 203 def page() check_secret Util.symbolize_keys! user_id = [:user_id].to_s 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 Util.isoify_dates! properties ensure_user user_id add_context context enqueue({ :userId => user_id, :name => name, :properties => properties, :context => context, :timestamp => .iso8601, :action => 'page' }) end |
#queued_messages ⇒ Object
public: Returns the number of queued messages
returns Fixnum of messages in the queue
267 268 269 |
# File 'lib/analytics-ruby/client.rb', line 267 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
:properties - Hash of screen properties (optional)
:timestamp - Time of when the screen occured (optional)
:context - Hash of context (optional)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/analytics-ruby/client.rb', line 237 def screen() check_secret Util.symbolize_keys! user_id = [:user_id].to_s 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 Util.isoify_dates! properties ensure_user user_id add_context context enqueue({ :userId => user_id, :name => name, :properties => properties, :context => context, :timestamp => .iso8601, :action => '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)
64 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 |
# File 'lib/analytics-ruby/client.rb', line 64 def track() check_secret Util.symbolize_keys! event = [:event] user_id = [:user_id].to_s properties = [:properties] || {} = [:timestamp] || Time.new context = [:context] || {} ensure_user user_id 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 Util.isoify_dates! properties add_context context enqueue({ :event => event, :userId => user_id, :context => context, :properties => properties, :timestamp => .iso8601, :action => 'track' }) end |