Class: AnalyticsRuby::Client
- Inherits:
-
Object
- Object
- AnalyticsRuby::Client
- Defined in:
- lib/analytics-ruby/client.rb
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.
-
#identify(options) ⇒ Object
public: Identifies a user.
-
#initialize(options = {}) ⇒ Client
constructor
public: Creates a new client.
-
#queued_messages ⇒ Object
public: Returns the number of queued messages.
-
#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
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/analytics-ruby/client.rb', line 19 def initialize( = {}) Util.symbolize_keys!() @queue = Queue.new @secret = [:secret] @max_queue_size = [:max_queue_size] || AnalyticsRuby::Defaults::Queue::MAX_SIZE check_secret @consumer = AnalyticsRuby::Consumer.new(@queue, @secret, ) @thread = Thread.new { @consumer.run } 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)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/analytics-ruby/client.rb', line 121 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
37 38 39 40 41 |
# File 'lib/analytics-ruby/client.rb', line 37 def flush while !@queue.empty? || @consumer.is_requesting? sleep(0.1) end 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)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/analytics-ruby/client.rb', line 89 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 add_context(context) enqueue({ userId: user_id, context: context, traits: traits, timestamp: .iso8601, action: 'identify' }) end |
#queued_messages ⇒ Object
public: Returns the number of queued messages
returns Fixnum of messages in the queue
148 149 150 |
# File 'lib/analytics-ruby/client.rb', line 148 def @queue.length 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)
51 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 |
# File 'lib/analytics-ruby/client.rb', line 51 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 add_context(context) enqueue({ event: event, userId: user_id, context: context, properties: properties, timestamp: .iso8601, action: 'track' }) end |