Method: Segment::Analytics::Client#track

Defined in:
lib/segment/analytics/client.rb

#track(attrs) ⇒ Object

public: Tracks an event

attrs - Hash

:anonymous_id - String of the user's id when you don't know who they are yet. (optional but you must provide either an anonymous_id or user_id. See: https://segment.io/docs/tracking - api/track/#user - id)
:context      - Hash of context. (optional)
:event        - String of event name.
:integrations - Hash specifying what integrations this event goes to. (optional)
:options      - Hash specifying options such as user traits. (optional)
:properties   - Hash of event properties. (optional)
:timestamp    - Time of when the event occurred. (optional)
:user_id      - String of the user id.


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
81
82
83
84
85
86
# File 'lib/segment/analytics/client.rb', line 55

def track attrs
  symbolize_keys! attrs
  check_user_id! attrs

  event = attrs[:event]
  properties = attrs[:properties] || {}
  timestamp = attrs[:timestamp] || Time.new
  context = attrs[:context] || {}

  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
  isoify_dates! properties

  add_context context

  enqueue({
    :event => event,
    :userId => attrs[:user_id],
    :anonymousId => attrs[:anonymous_id],
    :context =>  context,
    :options => attrs[:options],
    :integrations => attrs[:integrations],
    :properties => properties,
    :timestamp => datetime_in_iso8601(timestamp),
    :type => 'track'
  })
end