Class: Analytics::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/analytics/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)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/analytics/client.rb', line 17

def initialize (options = {})

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

  check_secret

  @consumer = Analytics::Consumer.new(@queue, @secret, options)
  Thread.new { @consumer.run }
end

Instance Method Details

#identify(options) ⇒ Object

Public: Identifies a user

options - Hash

:sessionId - String of the user session. (optional with userId)
:userId    - String of the user id. (optional with sessionId)
:context   - Hash of context. (optional)
:traits    - Hash of user traits. (optional)
:timestamp - Time of when the event occurred. (optional)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/analytics/client.rb', line 75

def identify(options)

  check_secret

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

  ensure_user(session_id, user_id)
  check_timestamp(timestamp)

  fail ArgumentError, "Must supply traits as a hash" unless traits.is_a? Hash

  add_context(context)

  enqueue({ sessionId: session_id,
            userId:    user_id,
            context:   context,
            traits:    traits,
            timestamp: timestamp.iso8601,
            action:    "identify" })
end

#track(options) ⇒ Object

Public: Tracks an event

options - Hash

:event      - String of event name.
:sessionId  - String of the user session. (optional with userId)
:userId     - String of the user id. (optional with sessionId)
:context    - Hash of context. (optional)
:properties - Hash of event properties. (optional)
:timestamp  - Time of when the event occurred. (optional)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/analytics/client.rb', line 38

def track(options)

  check_secret

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

  ensure_user(session_id, user_id)
  check_timestamp(timestamp)

  if event.nil? || event.empty?
    fail ArgumentError, "Must supply event as a non-empty string"
  end

  add_context(context)

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