Module: Omnitrack::Concerns::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/omnitrack/concerns/controller.rb

Overview

Include this concern in any controller to get convenient tracking helpers and automatic context enrichment.

class ApplicationController < ActionController::Base
  include Omnitrack::Controller
end

Instance Method Summary collapse

Instance Method Details

#omnitrack_contextObject

Access the current request context (already built by middleware)



20
21
22
# File 'lib/omnitrack/concerns/controller.rb', line 20

def omnitrack_context
  Omnitrack::Context.current || Omnitrack::Context.from_request(request)
end

#omnitrack_conversion(data = {}) ⇒ Object

Track a conversion from a controller action.



37
38
39
40
# File 'lib/omnitrack/concerns/controller.rb', line 37

def omnitrack_conversion(data = {})
  enriched = merge_context(data)
  Omnitrack.track_conversion(enriched)
end

#omnitrack_event(event_name, payload = {}) ⇒ Object

Track an event from a controller action.

def create
  @order = Order.create!(order_params)
  omnitrack_event("purchase", value: @order.total, currency: "USD")
end


31
32
33
34
# File 'lib/omnitrack/concerns/controller.rb', line 31

def omnitrack_event(event_name, payload = {})
  enriched = merge_context(payload)
  Omnitrack.track(event_name, enriched)
end

#omnitrack_identify(user_data = {}) ⇒ Object

Identify the current user.



43
44
45
# File 'lib/omnitrack/concerns/controller.rb', line 43

def omnitrack_identify(user_data = {})
  Omnitrack.identify(user_data)
end

#omnitrack_set(data = {}) ⇒ Object

Attach extra data to the current context (persists for this request only).



48
49
50
# File 'lib/omnitrack/concerns/controller.rb', line 48

def omnitrack_set(data = {})
  omnitrack_context.merge!(data)
end