Module: Gitlab::Experimentation::ControllerConcern

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController, Oauth::ApplicationsController, Oauth::AuthorizationsController
Defined in:
lib/gitlab/experimentation.rb

Overview

Controller concern that checks if an `experimentation_subject_id cookie` is present and sets it if absent. Used for A/B testing of experimental features. Exposes the `experiment_enabled?(experiment_name)` method to controllers and views. It returns true when the experiment is enabled and the user is selected as part of the experimental group.

Instance Method Summary collapse

Instance Method Details

#experiment_enabled?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)

94
95
96
97
98
99
100
101
# File 'lib/gitlab/experimentation.rb', line 94

def experiment_enabled?(experiment_key)
  return false if dnt_enabled?

  return true if Experimentation.enabled_for_user?(experiment_key, experimentation_subject_index)
  return true if forced_enabled?(experiment_key)

  false
end

#experiment_tracking_category_and_group(experiment_key) ⇒ Object


121
122
123
# File 'lib/gitlab/experimentation.rb', line 121

def experiment_tracking_category_and_group(experiment_key)
  "#{tracking_category(experiment_key)}:#{tracking_group(experiment_key, '_group')}"
end

#frontend_experimentation_tracking_data(experiment_key, action, value = nil) ⇒ Object


109
110
111
112
113
# File 'lib/gitlab/experimentation.rb', line 109

def frontend_experimentation_tracking_data(experiment_key, action, value = nil)
  track_experiment_event_for(experiment_key, action, value) do |tracking_data|
    gon.push(tracking_data: tracking_data)
  end
end

#record_experiment_user(experiment_key) ⇒ Object


115
116
117
118
119
# File 'lib/gitlab/experimentation.rb', line 115

def record_experiment_user(experiment_key)
  return unless Experimentation.enabled?(experiment_key) && current_user

  ::Experiment.add_user(experiment_key, tracking_group(experiment_key), current_user)
end

84
85
86
87
88
89
90
91
92
# File 'lib/gitlab/experimentation.rb', line 84

def set_experimentation_subject_id_cookie
  return if cookies[:experimentation_subject_id].present?

  cookies.permanent.signed[:experimentation_subject_id] = {
    value: SecureRandom.uuid,
    secure: ::Gitlab.config.gitlab.https,
    httponly: true
  }
end

#track_experiment_event(experiment_key, action, value = nil) ⇒ Object


103
104
105
106
107
# File 'lib/gitlab/experimentation.rb', line 103

def track_experiment_event(experiment_key, action, value = nil)
  track_experiment_event_for(experiment_key, action, value) do |tracking_data|
    ::Gitlab::Tracking.event(tracking_data.delete(:category), tracking_data.delete(:action), tracking_data)
  end
end