Class: AnalyticsRuby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/analytics-ruby/client.rb

Instance Method Summary collapse

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

  Util.symbolize_keys!(options)

  @queue = Queue.new
  @secret = options[:secret]
  @max_queue_size = options[:max_queue_size] || AnalyticsRuby::Defaults::Queue::MAX_SIZE

  check_secret

  @consumer = AnalyticsRuby::Consumer.new(@queue, @secret, options)
  @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(options)

  check_secret

  Util.symbolize_keys!(options)

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

  ensure_user(from)
  ensure_user(to)
  check_timestamp(timestamp)

  add_context(context)

  enqueue({ from:      from,
            to:        to,
            context:   context,
            timestamp: timestamp.iso8601,
            action:    'alias' })
end

#flushObject

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(options)

  check_secret

  Util.symbolize_keys!(options)

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

  ensure_user(user_id)
  check_timestamp(timestamp)

  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: timestamp.iso8601,
            action:    'identify' })
end

#queued_messagesObject

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 queued_messages
  @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(options)

  check_secret

  Util.symbolize_keys!(options)

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

  ensure_user(user_id)
  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

  add_context(context)

  enqueue({ event:      event,
            userId:     user_id,
            context:    context,
            properties: properties,
            timestamp:  timestamp.iso8601,
            action:     'track' })
end