2019-10-01 14:06:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == Experimentation
|
|
|
|
#
|
2020-04-21 11:21:10 -04:00
|
|
|
# Utility module for A/B testing experimental features. Define your experiments in the `EXPERIMENTS` constant.
|
|
|
|
# Experiment options:
|
|
|
|
# - environment (optional, defaults to enabled for development and GitLab.com)
|
|
|
|
# - tracking_category (optional, used to set the category when tracking an experiment event)
|
|
|
|
#
|
|
|
|
# The experiment is controlled by a Feature Flag (https://docs.gitlab.com/ee/development/feature_flags/controls.html),
|
2020-05-14 14:08:06 -04:00
|
|
|
# which is named "#{experiment_key}_experiment_percentage" and *must* be set with a percentage and not be used for other purposes.
|
2020-04-21 11:21:10 -04:00
|
|
|
#
|
2020-05-14 14:08:06 -04:00
|
|
|
# To enable the experiment for 10% of the users:
|
|
|
|
#
|
2020-06-05 11:08:23 -04:00
|
|
|
# chatops: `/chatops run feature set experiment_key_experiment_percentage 10`
|
|
|
|
# console: `Feature.enable_percentage_of_time(:experiment_key_experiment_percentage, 10)`
|
2020-04-21 11:21:10 -04:00
|
|
|
#
|
|
|
|
# To disable the experiment:
|
|
|
|
#
|
2020-05-14 14:08:06 -04:00
|
|
|
# chatops: `/chatops run feature delete experiment_key_experiment_percentage`
|
2020-06-02 08:08:33 -04:00
|
|
|
# console: `Feature.remove(:experiment_key_experiment_percentage)`
|
2020-05-14 14:08:06 -04:00
|
|
|
#
|
|
|
|
# To check the current rollout percentage:
|
|
|
|
#
|
|
|
|
# chatops: `/chatops run feature get experiment_key_experiment_percentage`
|
|
|
|
# console: `Feature.get(:experiment_key_experiment_percentage).percentage_of_time_value`
|
2019-10-01 14:06:28 -04:00
|
|
|
#
|
2020-06-02 08:08:33 -04:00
|
|
|
|
2020-06-05 11:08:23 -04:00
|
|
|
# TODO: see https://gitlab.com/gitlab-org/gitlab/-/issues/217490
|
2019-10-01 14:06:28 -04:00
|
|
|
module Gitlab
|
|
|
|
module Experimentation
|
|
|
|
EXPERIMENTS = {
|
|
|
|
signup_flow: {
|
2019-10-23 11:06:29 -04:00
|
|
|
tracking_category: 'Growth::Acquisition::Experiment::SignUpFlow'
|
2020-01-16 04:08:46 -05:00
|
|
|
},
|
2020-05-14 14:08:06 -04:00
|
|
|
onboarding_issues: {
|
|
|
|
tracking_category: 'Growth::Conversion::Experiment::OnboardingIssues'
|
|
|
|
},
|
2020-02-03 13:08:46 -05:00
|
|
|
suggest_pipeline: {
|
|
|
|
tracking_category: 'Growth::Expansion::Experiment::SuggestPipeline'
|
2020-03-24 08:09:42 -04:00
|
|
|
},
|
|
|
|
ci_notification_dot: {
|
|
|
|
tracking_category: 'Growth::Expansion::Experiment::CiNotificationDot'
|
2020-03-26 02:08:40 -04:00
|
|
|
},
|
2020-05-15 08:08:28 -04:00
|
|
|
upgrade_link_in_user_menu_a: {
|
|
|
|
tracking_category: 'Growth::Expansion::Experiment::UpgradeLinkInUserMenuA'
|
2020-06-05 08:08:24 -04:00
|
|
|
},
|
|
|
|
invite_members_version_a: {
|
|
|
|
tracking_category: 'Growth::Expansion::Experiment::InviteMembersVersionA'
|
2020-06-08 17:09:17 -04:00
|
|
|
},
|
|
|
|
new_create_project_ui: {
|
|
|
|
tracking_category: 'Manage::Import::Experiment::NewCreateProjectUi'
|
2020-08-05 08:09:45 -04:00
|
|
|
},
|
|
|
|
terms_opt_in: {
|
|
|
|
tracking_category: 'Growth::Acquisition::Experiment::TermsOptIn'
|
2020-08-07 11:10:17 -04:00
|
|
|
},
|
|
|
|
contact_sales_btn_in_app: {
|
|
|
|
tracking_category: 'Growth::Conversion::Experiment::ContactSalesInApp'
|
2020-08-18 17:09:57 -04:00
|
|
|
},
|
|
|
|
customize_homepage: {
|
|
|
|
tracking_category: 'Growth::Expansion::Experiment::CustomizeHomepage'
|
2020-08-25 14:10:49 -04:00
|
|
|
},
|
|
|
|
invite_email: {
|
|
|
|
tracking_category: 'Growth::Acquisition::Experiment::InviteEmail'
|
2019-10-01 14:06:28 -04:00
|
|
|
}
|
|
|
|
}.freeze
|
|
|
|
|
2020-08-10 17:09:44 -04:00
|
|
|
GROUP_CONTROL = :control
|
|
|
|
GROUP_EXPERIMENTAL = :experimental
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
# Controller concern that checks if an `experimentation_subject_id cookie` is present and sets it if absent.
|
2019-10-01 14:06:28 -04:00
|
|
|
# Used for A/B testing of experimental features. Exposes the `experiment_enabled?(experiment_name)` method
|
2019-10-23 11:06:29 -04:00
|
|
|
# to controllers and views. It returns true when the experiment is enabled and the user is selected as part
|
|
|
|
# of the experimental group.
|
2019-10-01 14:06:28 -04:00
|
|
|
#
|
|
|
|
module ControllerConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2020-03-09 23:08:03 -04:00
|
|
|
before_action :set_experimentation_subject_id_cookie, unless: :dnt_enabled?
|
2020-08-20 17:10:18 -04:00
|
|
|
helper_method :experiment_enabled?, :experiment_tracking_category_and_group
|
2019-10-01 14:06:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_experimentation_subject_id_cookie
|
|
|
|
return if cookies[:experimentation_subject_id].present?
|
|
|
|
|
|
|
|
cookies.permanent.signed[:experimentation_subject_id] = {
|
|
|
|
value: SecureRandom.uuid,
|
2019-10-28 08:06:50 -04:00
|
|
|
secure: ::Gitlab.config.gitlab.https,
|
|
|
|
httponly: true
|
2019-10-01 14:06:28 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-10-18 17:06:37 -04:00
|
|
|
def experiment_enabled?(experiment_key)
|
2020-03-09 23:08:03 -04:00
|
|
|
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
|
2019-10-23 11:06:29 -04:00
|
|
|
end
|
|
|
|
|
2020-01-21 13:07:31 -05:00
|
|
|
def track_experiment_event(experiment_key, action, value = nil)
|
|
|
|
track_experiment_event_for(experiment_key, action, value) do |tracking_data|
|
2019-10-23 11:06:29 -04:00
|
|
|
::Gitlab::Tracking.event(tracking_data.delete(:category), tracking_data.delete(:action), tracking_data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-21 13:07:31 -05:00
|
|
|
def frontend_experimentation_tracking_data(experiment_key, action, value = nil)
|
|
|
|
track_experiment_event_for(experiment_key, action, value) do |tracking_data|
|
2019-10-23 11:06:29 -04:00
|
|
|
gon.push(tracking_data: tracking_data)
|
|
|
|
end
|
2019-10-01 14:06:28 -04:00
|
|
|
end
|
|
|
|
|
2020-08-10 17:09:44 -04:00
|
|
|
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
|
|
|
|
|
2020-08-20 17:10:18 -04:00
|
|
|
def experiment_tracking_category_and_group(experiment_key)
|
|
|
|
"#{tracking_category(experiment_key)}:#{tracking_group(experiment_key, '_group')}"
|
|
|
|
end
|
|
|
|
|
2019-10-01 14:06:28 -04:00
|
|
|
private
|
|
|
|
|
2020-03-09 23:08:03 -04:00
|
|
|
def dnt_enabled?
|
|
|
|
Gitlab::Utils.to_boolean(request.headers['DNT'])
|
|
|
|
end
|
|
|
|
|
2019-10-23 11:06:29 -04:00
|
|
|
def experimentation_subject_id
|
|
|
|
cookies.signed[:experimentation_subject_id]
|
|
|
|
end
|
|
|
|
|
2019-10-01 14:06:28 -04:00
|
|
|
def experimentation_subject_index
|
|
|
|
return if experimentation_subject_id.blank?
|
|
|
|
|
|
|
|
experimentation_subject_id.delete('-').hex % 100
|
|
|
|
end
|
2019-10-23 11:06:29 -04:00
|
|
|
|
2020-01-21 13:07:31 -05:00
|
|
|
def track_experiment_event_for(experiment_key, action, value)
|
2019-10-23 11:06:29 -04:00
|
|
|
return unless Experimentation.enabled?(experiment_key)
|
|
|
|
|
2020-01-21 13:07:31 -05:00
|
|
|
yield experimentation_tracking_data(experiment_key, action, value)
|
2019-10-23 11:06:29 -04:00
|
|
|
end
|
|
|
|
|
2020-01-21 13:07:31 -05:00
|
|
|
def experimentation_tracking_data(experiment_key, action, value)
|
2019-10-23 11:06:29 -04:00
|
|
|
{
|
|
|
|
category: tracking_category(experiment_key),
|
|
|
|
action: action,
|
2020-08-20 17:10:18 -04:00
|
|
|
property: tracking_group(experiment_key, "_group"),
|
2020-01-21 13:07:31 -05:00
|
|
|
label: experimentation_subject_id,
|
|
|
|
value: value
|
|
|
|
}.compact
|
2019-10-23 11:06:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tracking_category(experiment_key)
|
|
|
|
Experimentation.experiment(experiment_key).tracking_category
|
|
|
|
end
|
|
|
|
|
2020-08-20 17:10:18 -04:00
|
|
|
def tracking_group(experiment_key, suffix = nil)
|
2019-10-23 11:06:29 -04:00
|
|
|
return unless Experimentation.enabled?(experiment_key)
|
|
|
|
|
2020-08-20 17:10:18 -04:00
|
|
|
group = experiment_enabled?(experiment_key) ? GROUP_EXPERIMENTAL : GROUP_CONTROL
|
|
|
|
|
|
|
|
suffix ? "#{group}#{suffix}" : group
|
2019-10-23 11:06:29 -04:00
|
|
|
end
|
2019-10-24 14:07:05 -04:00
|
|
|
|
|
|
|
def forced_enabled?(experiment_key)
|
|
|
|
params.has_key?(:force_experiment) && params[:force_experiment] == experiment_key.to_s
|
|
|
|
end
|
2019-10-01 14:06:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2019-10-18 17:06:37 -04:00
|
|
|
def experiment(key)
|
|
|
|
Experiment.new(EXPERIMENTS[key].merge(key: key))
|
|
|
|
end
|
|
|
|
|
2019-10-23 11:06:29 -04:00
|
|
|
def enabled?(experiment_key)
|
2019-10-01 14:06:28 -04:00
|
|
|
return false unless EXPERIMENTS.key?(experiment_key)
|
|
|
|
|
2019-10-18 17:06:37 -04:00
|
|
|
experiment = experiment(experiment_key)
|
2020-04-21 11:21:10 -04:00
|
|
|
experiment.enabled? && experiment.enabled_for_environment?
|
2019-10-23 11:06:29 -04:00
|
|
|
end
|
2019-10-01 14:06:28 -04:00
|
|
|
|
2019-10-23 11:06:29 -04:00
|
|
|
def enabled_for_user?(experiment_key, experimentation_subject_index)
|
|
|
|
enabled?(experiment_key) &&
|
|
|
|
experiment(experiment_key).enabled_for_experimentation_subject?(experimentation_subject_index)
|
2019-10-01 14:06:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
Experiment = Struct.new(:key, :environment, :tracking_category, keyword_init: true) do
|
|
|
|
def enabled?
|
2020-08-05 11:09:59 -04:00
|
|
|
experiment_percentage > 0
|
2019-10-01 14:06:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def enabled_for_environment?
|
2020-04-21 11:21:10 -04:00
|
|
|
return ::Gitlab.dev_env_or_com? if environment.nil?
|
2019-10-01 14:06:28 -04:00
|
|
|
|
|
|
|
environment
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled_for_experimentation_subject?(experimentation_subject_index)
|
2020-04-21 11:21:10 -04:00
|
|
|
return false if experimentation_subject_index.blank?
|
|
|
|
|
|
|
|
experimentation_subject_index <= experiment_percentage
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2019-10-01 14:06:28 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
# When a feature does not exist, the `percentage_of_time_value` method will return 0
|
|
|
|
def experiment_percentage
|
2020-06-02 08:08:33 -04:00
|
|
|
@experiment_percentage ||= Feature.get(:"#{key}_experiment_percentage").percentage_of_time_value # rubocop:disable Gitlab/AvoidFeatureGet
|
2019-10-01 14:06:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|