Module: ThisData::TrackRequest

Defined in:
lib/this_data/track_request.rb

Defined Under Namespace

Classes: ThisDataTrackError

Instance Method Summary collapse

Instance Method Details

#thisdata_track(verb: ThisData::Verbs::LOG_IN, user: nil, authenticated: nil) ⇒ Object

Will pull request and user details from the controller, and send an event to ThisData. Arguments:

verb: (String, Required). Defaults to ThisData::Verbs::LOG_IN.
user: (Object, Optional). If you want to override the user record
  that we would usually fetch, you can pass it here.
  Unless a user is specified here we'll attempt to get the user record
    as specified in the ThisData gem configuration. This defaults to
    `current_user`.
  The object must respond to at least
    `ThisData.configuration.user_id_method`, which defaults to `id`.
authenticated: (Boolean, Optional, default nil). Used to indicate
  whether a user is authenticated or not. By default we assume that if
  there is a user specified then they are authenticated, but in some
  cases (like a log-in-denied event) you might want to track the user
  but tell us that they are not authenticated.
  In these situations you should set `authenticated` to false.

Returns the result of ThisData.track (an HTTPartyResponse)



31
32
33
34
35
36
37
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
# File 'lib/this_data/track_request.rb', line 31

def thisdata_track(verb: ThisData::Verbs::LOG_IN, user: nil, authenticated: nil)
  # Fetch the user unless it's been overridden
  if user.nil?
    user = send(ThisData.configuration.user_method)
  end

  # Get a Hash of details for the user
  user_details = user_details(user)

  # If specified, set the authenticated state of the user
  unless authenticated.nil?
    user_details.merge!(authenticated: authenticated)
  end

  event = {
    verb:       verb,
    ip:         request.remote_ip,
    user_agent: request.user_agent,
    user:       user_details,
    session: {
      td_cookie_expected: ThisData.configuration.expect_js_cookie,
      td_cookie_id:       td_cookie_value,
    }
  }

  ThisData.track(event)
rescue => e
  ThisData.error "Could not track event:"
  ThisData.error e
  ThisData.error e.backtrace[0..5].join("\n")
  false
end