Class: Analytics::Client
- Inherits:
-
Object
- Object
- Analytics::Client
- Defined in:
- lib/analytics/client.rb
Instance Method Summary collapse
-
#identify(options) ⇒ Object
Public: Identifies a user.
-
#initialize(options = {}) ⇒ Client
constructor
Public: Creates a new client.
-
#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)
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/analytics/client.rb', line 17 def initialize ( = {}) @queue = Queue.new @secret = [:secret] @max_queue_size = [:max_queue_size] || Analytics::Defaults::Queue::MAX_SIZE check_secret @consumer = Analytics::Consumer.new(@queue, @secret, ) 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() check_secret session_id = [:session_id] user_id = [:user_id] context = [:context] || {} traits = [:traits] || {} = [:timestamp] || Time.new ensure_user(session_id, user_id) () 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: .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() check_secret event = [:event] session_id = [:session_id] user_id = [:user_id] context = [:context] || {} properties = [:properties] || {} = [:timestamp] || Time.new ensure_user(session_id, user_id) () 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: .iso8601, action: "track" }) end |