Class: June::Analytics::Client

Inherits:
Object
  • Object
show all
Includes:
Logging, Utils
Defined in:
lib/june/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 Logging

included, #logger

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

Returns a new instance of Client.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :write_key (String)

    Your project’s write_key

  • :max_queue_size (FixNum)

    Maximum number of calls to be remain queued.

  • :on_error (Proc)

    Handles error calls from the API.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/june/analytics/client.rb', line 20

def initialize(opts = {})
  symbolize_keys!(opts)

  @queue = Queue.new
  @test = opts[:test]
  @write_key = opts[:write_key]
  @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE
  @worker_mutex = Mutex.new
  @worker = Worker.new(@queue, @write_key, opts)
  @worker_thread = nil

  check_write_key!

  at_exit { @worker_thread && @worker_thread[:should_exit] = true }
end

Instance Method Details

#alias(attrs) ⇒ Object

Aliases a user from one id to another

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :previous_id (String)

    The ID to alias from

  • :anonymous_id (String)

    ID for a user when you don’t know who they are yet. (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :context (Hash) — default: {}
  • :integrations (Hash)

    What integrations this event goes to (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :user_id (String)

    The ID for this user in your database (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :options (Hash)

    Options such as user traits (optional)

See Also:



96
97
98
99
# File 'lib/june/analytics/client.rb', line 96

def alias(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_alias(attrs))
end

#flushObject

Synchronously waits until the worker has flushed the queue.

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



40
41
42
43
44
45
# File 'lib/june/analytics/client.rb', line 40

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

#group(attrs) ⇒ Object

Associates a user identity with a group.

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :group_id (String)

    The ID of the group

  • :traits (Hash)

    User traits (optional)

  • :anonymous_id (String)

    ID for a user when you don’t know who they are yet. (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :context (Hash) — default: {}
  • :integrations (Hash)

    What integrations this event goes to (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :user_id (String)

    The ID for this user in your database (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :options (Hash)

    Options such as user traits (optional)

See Also:



110
111
112
113
# File 'lib/june/analytics/client.rb', line 110

def group(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_group(attrs))
end

#identify(attrs) ⇒ Object

Identifies a user

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :traits (Hash)

    User traits (optional)

  • :anonymous_id (String)

    ID for a user when you don’t know who they are yet. (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :context (Hash) — default: {}
  • :integrations (Hash)

    What integrations this event goes to (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :user_id (String)

    The ID for this user in your database (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :options (Hash)

    Options such as user traits (optional)

See Also:



83
84
85
86
# File 'lib/june/analytics/client.rb', line 83

def identify(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_identify(attrs))
end

#page(attrs) ⇒ Object

Records a page view

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :name (String)

    Name of the page

  • :properties (Hash)

    Page properties (optional)

  • :anonymous_id (String)

    ID for a user when you don’t know who they are yet. (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :context (Hash) — default: {}
  • :integrations (Hash)

    What integrations this event goes to (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :user_id (String)

    The ID for this user in your database (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :options (Hash)

    Options such as user traits (optional)

See Also:



124
125
126
127
# File 'lib/june/analytics/client.rb', line 124

def page(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_page(attrs))
end

#queued_messagesFixnum

Returns number of messages in the queue.

Returns:

  • (Fixnum)

    number of messages in the queue



143
144
145
# File 'lib/june/analytics/client.rb', line 143

def queued_messages
  @queue.length
end

#screen(attrs) ⇒ Object

Records a screen view (for a mobile app)

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :name (String)

    Name of the screen

  • :properties (Hash)

    Screen properties (optional)

  • :category (String)

    The screen category (optional)

  • :anonymous_id (String)

    ID for a user when you don’t know who they are yet. (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :context (Hash) — default: {}
  • :integrations (Hash)

    What integrations this event goes to (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :user_id (String)

    The ID for this user in your database (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :options (Hash)

    Options such as user traits (optional)



137
138
139
140
# File 'lib/june/analytics/client.rb', line 137

def screen(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_screen(attrs))
end

#test_queueObject



147
148
149
150
151
152
153
# File 'lib/june/analytics/client.rb', line 147

def test_queue
  unless @test
    raise 'Test queue only available when setting :test to true.'
  end

  @test_queue ||= TestQueue.new
end

#track(attrs) ⇒ Object

Tracks an event

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :event (String)

    Event name

  • :properties (Hash)

    Event properties (optional)

  • :anonymous_id (String)

    ID for a user when you don’t know who they are yet. (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :context (Hash) — default: {}
  • :integrations (Hash)

    What integrations this event goes to (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :user_id (String)

    The ID for this user in your database (optional but you must provide either an ‘anonymous_id` or `user_id`)

  • :options (Hash)

    Options such as user traits (optional)

See Also:



70
71
72
73
# File 'lib/june/analytics/client.rb', line 70

def track(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_track(attrs))
end