debian-mirror-gitlab/lib/gitlab/experimentation.rb

142 lines
5.1 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
# == Experimentation
#
2020-05-24 23:13:21 +05:30
# 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)
2021-01-29 00:20:46 +05:30
# - use_backwards_compatible_subject_index (optional, set this to true if you need backwards compatibility)
2020-05-24 23:13:21 +05:30
#
# The experiment is controlled by a Feature Flag (https://docs.gitlab.com/ee/development/feature_flags/controls.html),
# which is named "#{experiment_key}_experiment_percentage" and *must* be set with a percentage and not be used for other purposes.
#
# To enable the experiment for 10% of the users:
#
# chatops: `/chatops run feature set experiment_key_experiment_percentage 10`
2020-06-23 00:09:42 +05:30
# console: `Feature.enable_percentage_of_time(:experiment_key_experiment_percentage, 10)`
2020-05-24 23:13:21 +05:30
#
# To disable the experiment:
#
# chatops: `/chatops run feature delete experiment_key_experiment_percentage`
2020-06-23 00:09:42 +05:30
# console: `Feature.remove(:experiment_key_experiment_percentage)`
2020-05-24 23:13:21 +05:30
#
# 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-12-21 20:55:43 +05:30
#
2020-06-23 00:09:42 +05:30
# TODO: see https://gitlab.com/gitlab-org/gitlab/-/issues/217490
2019-12-21 20:55:43 +05:30
module Gitlab
module Experimentation
EXPERIMENTS = {
2020-05-24 23:13:21 +05:30
onboarding_issues: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Conversion::Experiment::OnboardingIssues',
use_backwards_compatible_subject_index: true
2020-04-22 19:07:51 +05:30
},
ci_notification_dot: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Expansion::Experiment::CiNotificationDot',
use_backwards_compatible_subject_index: true
2020-04-22 19:07:51 +05:30
},
2020-05-24 23:13:21 +05:30
upgrade_link_in_user_menu_a: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Expansion::Experiment::UpgradeLinkInUserMenuA',
use_backwards_compatible_subject_index: true
2020-06-23 00:09:42 +05:30
},
invite_members_version_a: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Expansion::Experiment::InviteMembersVersionA',
use_backwards_compatible_subject_index: true
2020-06-23 00:09:42 +05:30
},
2021-01-03 14:25:43 +05:30
invite_members_version_b: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Expansion::Experiment::InviteMembersVersionB',
use_backwards_compatible_subject_index: true
},
invite_members_empty_group_version_a: {
tracking_category: 'Growth::Expansion::Experiment::InviteMembersEmptyGroupVersionA',
use_backwards_compatible_subject_index: true
2021-01-03 14:25:43 +05:30
},
2020-06-23 00:09:42 +05:30
new_create_project_ui: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Manage::Import::Experiment::NewCreateProjectUi',
use_backwards_compatible_subject_index: true
2020-10-24 23:57:45 +05:30
},
contact_sales_btn_in_app: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Conversion::Experiment::ContactSalesInApp',
use_backwards_compatible_subject_index: true
2020-10-24 23:57:45 +05:30
},
customize_homepage: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Expansion::Experiment::CustomizeHomepage',
use_backwards_compatible_subject_index: true
2020-11-24 15:15:51 +05:30
},
invite_email: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Acquisition::Experiment::InviteEmail',
use_backwards_compatible_subject_index: true
2021-01-03 14:25:43 +05:30
},
invitation_reminders: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Acquisition::Experiment::InvitationReminders',
use_backwards_compatible_subject_index: true
2021-01-03 14:25:43 +05:30
},
group_only_trials: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Conversion::Experiment::GroupOnlyTrials',
use_backwards_compatible_subject_index: true
2021-01-03 14:25:43 +05:30
},
default_to_issues_board: {
2021-01-29 00:20:46 +05:30
tracking_category: 'Growth::Conversion::Experiment::DefaultToIssuesBoard',
use_backwards_compatible_subject_index: true
2019-12-21 20:55:43 +05:30
}
}.freeze
class << self
def experiment(key)
Experiment.new(EXPERIMENTS[key].merge(key: key))
end
2019-12-26 22:10:19 +05:30
def enabled?(experiment_key)
2019-12-21 20:55:43 +05:30
return false unless EXPERIMENTS.key?(experiment_key)
experiment = experiment(experiment_key)
2021-01-29 00:20:46 +05:30
experiment.enabled_for_environment? && experiment.enabled?
2019-12-26 22:10:19 +05:30
end
2019-12-21 20:55:43 +05:30
2021-01-03 14:25:43 +05:30
def enabled_for_attribute?(experiment_key, attribute)
index = Digest::SHA1.hexdigest(attribute).hex % 100
enabled_for_value?(experiment_key, index)
end
2021-01-29 00:20:46 +05:30
def enabled_for_value?(experiment_key, value)
enabled?(experiment_key) && experiment(experiment_key).enabled_for_index?(value)
2019-12-21 20:55:43 +05:30
end
end
2021-01-29 00:20:46 +05:30
Experiment = Struct.new(
:key,
:environment,
:tracking_category,
:use_backwards_compatible_subject_index,
keyword_init: true
) do
2020-05-24 23:13:21 +05:30
def enabled?
2020-10-24 23:57:45 +05:30
experiment_percentage > 0
2019-12-21 20:55:43 +05:30
end
def enabled_for_environment?
2020-05-24 23:13:21 +05:30
return ::Gitlab.dev_env_or_com? if environment.nil?
2019-12-21 20:55:43 +05:30
environment
end
2021-01-03 14:25:43 +05:30
def enabled_for_index?(index)
return false if index.blank?
2020-05-24 23:13:21 +05:30
2021-01-03 14:25:43 +05:30
index <= experiment_percentage
2020-05-24 23:13:21 +05:30
end
private
2019-12-21 20:55:43 +05:30
2020-05-24 23:13:21 +05:30
# When a feature does not exist, the `percentage_of_time_value` method will return 0
def experiment_percentage
2020-06-23 00:09:42 +05:30
@experiment_percentage ||= Feature.get(:"#{key}_experiment_percentage").percentage_of_time_value # rubocop:disable Gitlab/AvoidFeatureGet
2019-12-21 20:55:43 +05:30
end
end
end
end