2020-11-10 19:08:58 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
# Controller concern that checks if an `experimentation_subject_id cookie` is present and sets it if absent.
|
2020-12-02 19:09:53 -05:00
|
|
|
# Used for A/B testing of experimental features. Exposes the `experiment_enabled?(experiment_name, subject: nil)` method
|
2020-11-10 19:08:58 -05:00
|
|
|
# to controllers and views. It returns true when the experiment is enabled and the user is selected as part
|
|
|
|
# of the experimental group.
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module Experimentation
|
|
|
|
module ControllerConcern
|
|
|
|
include ::Gitlab::Experimentation::GroupTypes
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_action :set_experimentation_subject_id_cookie, unless: :dnt_enabled?
|
2020-12-22 16:10:06 -05:00
|
|
|
helper_method :experiment_enabled?, :experiment_tracking_category_and_group, :tracking_label
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def push_frontend_experiment(experiment_key, subject: nil)
|
2020-11-10 19:08:58 -05:00
|
|
|
var_name = experiment_key.to_s.camelize(:lower)
|
2020-12-02 19:09:53 -05:00
|
|
|
|
|
|
|
enabled = experiment_enabled?(experiment_key, subject: subject)
|
2020-11-10 19:08:58 -05:00
|
|
|
|
|
|
|
gon.push({ experiments: { var_name => enabled } }, true)
|
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def experiment_enabled?(experiment_key, subject: nil)
|
|
|
|
return true if forced_enabled?(experiment_key)
|
2020-11-10 19:08:58 -05:00
|
|
|
return false if dnt_enabled?
|
|
|
|
|
2021-01-19 13:11:04 -05:00
|
|
|
Experimentation.log_invalid_rollout(experiment_key, subject)
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
subject ||= fallback_experimentation_subject_index(experiment_key)
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
Experimentation.in_experiment_group?(experiment_key, subject: subject)
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def track_experiment_event(experiment_key, action, value = nil, subject: nil)
|
2020-11-10 19:08:58 -05:00
|
|
|
return if dnt_enabled?
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
track_experiment_event_for(experiment_key, action, value, subject: subject) do |tracking_data|
|
2020-11-10 19:08:58 -05:00
|
|
|
::Gitlab::Tracking.event(tracking_data.delete(:category), tracking_data.delete(:action), **tracking_data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def frontend_experimentation_tracking_data(experiment_key, action, value = nil, subject: nil)
|
2020-11-10 19:08:58 -05:00
|
|
|
return if dnt_enabled?
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
track_experiment_event_for(experiment_key, action, value, subject: subject) do |tracking_data|
|
2020-11-10 19:08:58 -05:00
|
|
|
gon.push(tracking_data: tracking_data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-03 16:09:35 -05:00
|
|
|
def record_experiment_user(experiment_key, context = {})
|
2020-11-10 19:08:58 -05:00
|
|
|
return if dnt_enabled?
|
2020-12-02 19:09:53 -05:00
|
|
|
return unless Experimentation.active?(experiment_key) && current_user
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2021-01-19 13:11:04 -05:00
|
|
|
subject = Experimentation.rollout_strategy(experiment_key) == :cookie ? nil : current_user
|
|
|
|
|
|
|
|
::Experiment.add_user(experiment_key, tracking_group(experiment_key, nil, subject: subject), current_user, context)
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-11-25 19:09:35 -05:00
|
|
|
def record_experiment_conversion_event(experiment_key)
|
|
|
|
return if dnt_enabled?
|
|
|
|
return unless current_user
|
2020-12-02 19:09:53 -05:00
|
|
|
return unless Experimentation.active?(experiment_key)
|
2020-11-25 19:09:35 -05:00
|
|
|
|
|
|
|
::Experiment.record_conversion_event(experiment_key, current_user)
|
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def experiment_tracking_category_and_group(experiment_key, subject: nil)
|
|
|
|
"#{tracking_category(experiment_key)}:#{tracking_group(experiment_key, '_group', subject: subject)}"
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def dnt_enabled?
|
|
|
|
Gitlab::Utils.to_boolean(request.headers['DNT'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def experimentation_subject_id
|
|
|
|
cookies.signed[:experimentation_subject_id]
|
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def fallback_experimentation_subject_index(experiment_key)
|
2020-11-10 19:08:58 -05:00
|
|
|
return if experimentation_subject_id.blank?
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
if Experimentation.get_experiment(experiment_key).use_backwards_compatible_subject_index
|
|
|
|
experimentation_subject_id.delete('-')
|
2020-11-10 19:08:58 -05:00
|
|
|
else
|
2020-12-02 19:09:53 -05:00
|
|
|
experimentation_subject_id
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def track_experiment_event_for(experiment_key, action, value, subject: nil)
|
|
|
|
return unless Experimentation.active?(experiment_key)
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
yield experimentation_tracking_data(experiment_key, action, value, subject: subject)
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def experimentation_tracking_data(experiment_key, action, value, subject: nil)
|
2020-11-10 19:08:58 -05:00
|
|
|
{
|
|
|
|
category: tracking_category(experiment_key),
|
|
|
|
action: action,
|
2020-12-02 19:09:53 -05:00
|
|
|
property: tracking_group(experiment_key, "_group", subject: subject),
|
|
|
|
label: tracking_label(subject),
|
2020-11-10 19:08:58 -05:00
|
|
|
value: value
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def tracking_category(experiment_key)
|
2020-12-02 19:09:53 -05:00
|
|
|
Experimentation.get_experiment(experiment_key).tracking_category
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def tracking_group(experiment_key, suffix = nil, subject: nil)
|
|
|
|
return unless Experimentation.active?(experiment_key)
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
subject ||= fallback_experimentation_subject_index(experiment_key)
|
|
|
|
group = experiment_enabled?(experiment_key, subject: subject) ? GROUP_EXPERIMENTAL : GROUP_CONTROL
|
2020-11-10 19:08:58 -05:00
|
|
|
|
|
|
|
suffix ? "#{group}#{suffix}" : group
|
|
|
|
end
|
|
|
|
|
|
|
|
def forced_enabled?(experiment_key)
|
2020-12-21 13:10:26 -05:00
|
|
|
return true if params.has_key?(:force_experiment) && params[:force_experiment] == experiment_key.to_s
|
|
|
|
return false if cookies[:force_experiment].blank?
|
|
|
|
|
|
|
|
cookies[:force_experiment].to_s.split(',').any? { |experiment| experiment.strip == experiment_key.to_s }
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
2020-12-02 19:09:53 -05:00
|
|
|
|
2021-01-20 19:11:07 -05:00
|
|
|
def tracking_label(subject = nil)
|
2020-12-02 19:09:53 -05:00
|
|
|
return experimentation_subject_id if subject.blank?
|
|
|
|
|
|
|
|
if subject.respond_to?(:to_global_id)
|
|
|
|
Digest::MD5.hexdigest(subject.to_global_id.to_s)
|
|
|
|
else
|
|
|
|
Digest::MD5.hexdigest(subject.to_s)
|
|
|
|
end
|
|
|
|
end
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|