Module: AbPanel::ControllerAdditions
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/ab_panel/controller_additions.rb
Instance Method Summary collapse
- #ab_panel_options ⇒ Object
-
#distinct_id ⇒ Object
This sets a unique id for this user.
-
#initialize_ab_panel!(options = {}) ⇒ Object
Initializes AbPanel’s environment.
-
#track_action(name, properties = {}) ⇒ Object
Track controller actions visits.
-
#track_variable(name, value) ⇒ Object
Track a single variable.
-
#track_variables(variables = {}) ⇒ Object
Track multiple variables at once.
Instance Method Details
#ab_panel_options ⇒ Object
46 47 48 |
# File 'lib/ab_panel/controller_additions.rb', line 46 def @ab_panel_options ||= {} end |
#distinct_id ⇒ Object
This sets a unique id for this user.
You could override this in your ApplicationController to use your own implementation, e.g.:
`current_user.id` for logged in users.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ab_panel/controller_additions.rb', line 29 def distinct_id distinct_id = .signed['distinct_id'] return distinct_id if distinct_id distinct_id = (0..4).map { |i| i.even? ? ('A'..'Z').to_a[rand(26)] : rand(10) }.join .signed['distinct_id'] = { value: distinct_id, httponly: true, secure: request.ssl? } distinct_id end |
#initialize_ab_panel!(options = {}) ⇒ Object
Initializes AbPanel’s environment.
Typically, this would go in a global before filter.
class ApplicationController < ActionController::Base
before_filter :initialize_ab_panel!
end
This makes sure an ab_panel session is re-initialized on every request. Experiment conditions and unique user id are preserved in the user’s session.
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 87 88 89 90 91 92 |
# File 'lib/ab_panel/controller_additions.rb', line 61 def initialize_ab_panel!( = {}) AbPanel.reset! AbPanel.conditions = if .signed[:ab_panel_conditions] JSON.parse(.signed[:ab_panel_conditions]) else nil end .signed[:ab_panel_conditions] = { value: AbPanel.serialized_conditions, httponly: true, secure: request.ssl? } AbPanel.funnels = Set.new(.signed[:ab_panel_funnels]) .signed[:ab_panel_funnels] = { value: AbPanel.funnels, httponly: true, secure: request.ssl? } { 'distinct_id' => distinct_id, 'rack.session' => request.env['rack.session'], 'ip' => request.remote_ip }.each do |key, value| AbPanel.set_env(key, value) end end |
#track_action(name, properties = {}) ⇒ Object
Track controller actions visits.
name - The name of the event in Mixpanel. properties - The properties to be associated with the event.
Example:
def show
track_action '[visits] Course', { :course_id => @course.id }
end
This will track the event with the given name on CoursesController#show with the passed in attributes.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ab_panel/controller_additions.rb', line 107 def track_action(name, properties = {}) AbPanel.add_funnel(properties.delete(:funnel)) = { distinct_id: distinct_id, time: Time.now.utc, }.merge(properties) AbPanel.funnels.each do |funnel| ["funnel_#{funnel}"] = true end AbPanel.experiments.each do |exp| [exp] = AbPanel.conditions.send(exp).condition rescue nil end .merge!() AbPanel.set_env(:properties, ) AbPanel.identify(distinct_id) AbPanel.track(name, .merge()) end |
#track_variable(name, value) ⇒ Object
Track a single variable
Example:
track_variable :name, value
9 10 11 |
# File 'lib/ab_panel/controller_additions.rb', line 9 def track_variable(name, value) [name.to_sym] = value end |
#track_variables(variables = {}) ⇒ Object
Track multiple variables at once.
Example:
track_variables { foo: 'bar', ping: 'pong'}
17 18 19 20 21 |
# File 'lib/ab_panel/controller_additions.rb', line 17 def track_variables(variables={}) variables.each do |key, val| track_variable key, val end end |