Module: Gitlab::Experimentation::ControllerConcern

Extended by:
ActiveSupport::Concern
Includes:
GroupTypes, Tracking::Helpers
Included in:
ApplicationController, Oauth::ApplicationsController, Oauth::AuthorizationsController
Defined in:
lib/gitlab/experimentation/controller_concern.rb

Constant Summary

Constants included from GroupTypes

GroupTypes::GROUP_CONTROL, GroupTypes::GROUP_EXPERIMENTAL

Instance Method Summary collapse

Methods included from Tracking::Helpers

#dnt_enabled?, #trackable_html_request?

Instance Method Details

#experiment_enabled?(experiment_key, subject: nil) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
48
49
50
51
52
53
54
# File 'lib/gitlab/experimentation/controller_concern.rb', line 45

def experiment_enabled?(experiment_key, subject: nil)
  return true if forced_enabled?(experiment_key)
  return false if dnt_enabled?

  Experimentation.log_invalid_rollout(experiment_key, subject)

  subject ||= experimentation_subject_id

  Experimentation.in_experiment_group?(experiment_key, subject: subject)
end

#experiment_tracking_category_and_group(experiment_key, subject: nil) ⇒ Object


99
100
101
# File 'lib/gitlab/experimentation/controller_concern.rb', line 99

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

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


64
65
66
67
68
69
70
# File 'lib/gitlab/experimentation/controller_concern.rb', line 64

def frontend_experimentation_tracking_data(experiment_key, action, value = nil, subject: nil)
  return if dnt_enabled?

  track_experiment_event_for(experiment_key, action, value, subject: subject) do |tracking_data|
    gon.push(tracking_data: tracking_data)
  end
end

#push_frontend_experiment(experiment_key, subject: nil) ⇒ Object


37
38
39
40
41
42
43
# File 'lib/gitlab/experimentation/controller_concern.rb', line 37

def push_frontend_experiment(experiment_key, subject: nil)
  var_name = experiment_key.to_s.camelize(:lower)

  enabled = experiment_enabled?(experiment_key, subject: subject)

  gon.push({ experiments: { var_name => enabled } }, true)
end

#record_experiment_conversion_event(experiment_key, context = {}) ⇒ Object


91
92
93
94
95
96
97
# File 'lib/gitlab/experimentation/controller_concern.rb', line 91

def record_experiment_conversion_event(experiment_key, context = {})
  return if dnt_enabled?
  return unless current_user
  return unless Experimentation.active?(experiment_key)

  ::Experiment.record_conversion_event(experiment_key, current_user, context)
end

#record_experiment_group(experiment_key, group) ⇒ Object


81
82
83
84
85
86
87
88
89
# File 'lib/gitlab/experimentation/controller_concern.rb', line 81

def record_experiment_group(experiment_key, group)
  return if dnt_enabled?
  return unless Experimentation.active?(experiment_key) && group

  variant_subject = Experimentation.rollout_strategy(experiment_key) == :cookie ? nil : group
  variant = tracking_group(experiment_key, nil, subject: variant_subject)

  ::Experiment.add_group(experiment_key, group: group, variant: variant)
end

#record_experiment_user(experiment_key, context = {}) ⇒ Object


72
73
74
75
76
77
78
79
# File 'lib/gitlab/experimentation/controller_concern.rb', line 72

def record_experiment_user(experiment_key, context = {})
  return if dnt_enabled?
  return unless Experimentation.active?(experiment_key) && current_user

  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)
end

22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/experimentation/controller_concern.rb', line 22

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

    cookies.permanent.signed[:experimentation_subject_id] = {
      value: SecureRandom.uuid,
      secure: ::Gitlab.config.gitlab.https,
      httponly: true
    }
  else
    # We set the cookie before, although experiments are not conducted on self managed instances.
    cookies.delete(:experimentation_subject_id)
  end
end

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


56
57
58
59
60
61
62
# File 'lib/gitlab/experimentation/controller_concern.rb', line 56

def track_experiment_event(experiment_key, action, value = nil, subject: nil)
  return if dnt_enabled?

  track_experiment_event_for(experiment_key, action, value, subject: subject) do |tracking_data|
    ::Gitlab::Tracking.event(tracking_data.delete(:category), tracking_data.delete(:action), **tracking_data.merge!(user: current_user))
  end
end