class Exvo::Metrics

Constants

VERSION

Public Class Methods

new(user, request) click to toggle source
# File lib/exvo/metrics.rb, line 9
def initialize(user, request)
  @user, @request = user, request
end

Public Instance Methods

identify(user) click to toggle source
# File lib/exvo/metrics.rb, line 13
def identify(user)
  @user = user
  append_identification
end
method_missing(method, *args, &block) click to toggle source
# File lib/exvo/metrics.rb, line 28
def method_missing(method, *args, &block)
  metrics_platform.public_send(method, *args, &block)
end
track(event_name, properties = {}) click to toggle source
# File lib/exvo/metrics.rb, line 18
def track(event_name, properties = {})
  append_identification unless identified?
  metrics_platform.append_track(event_name, properties)
end
track_anonymously(event_name, properties = {}) click to toggle source

Track things without identifying the user. Useful for page view/site visit trakcing

# File lib/exvo/metrics.rb, line 24
def track_anonymously(event_name, properties = {})
  metrics_platform.append_track(event_name, properties)
end

Private Instance Methods

append_identification() click to toggle source
# File lib/exvo/metrics.rb, line 47
def append_identification
  # identify the user
  metrics_platform.append_identify(@user.nickname)

  # name this user in Stream view
  metrics_platform.append("name_tag", @user.nickname)

  # set properties of this user
  metrics_platform.append_set(user_properties)

  mark_as_identified
end
identified?() click to toggle source
# File lib/exvo/metrics.rb, line 43
def identified?
  @identified
end
mark_as_identified() click to toggle source
# File lib/exvo/metrics.rb, line 73
def mark_as_identified
  @identified = true
end
metrics_platform() click to toggle source
# File lib/exvo/metrics.rb, line 34
def metrics_platform
  @metrics_platform ||=
    if defined?(Rails) && Rails.application.config.middleware.include?("Mixpanel::Middleware")
      Mixpanel::Tracker.new(ENV['MIXPANEL_API_TOKEN'], { env: @request.env, persist: true })
    else
      Exvo::DummyMetrics.new
    end
end
user_properties() click to toggle source
# File lib/exvo/metrics.rb, line 60
def user_properties
  {
    id: @user.id,
    email: @user.email,
    first_name: @user.first_name,
    last_name: @user.last_name,
    username: @user.nickname,
    theme: @user.theme,
    country_code: @user.country_code,
    created: @user.created_at
  }
end