summaryrefslogtreecommitdiff
path: root/doc/development/experiment_guide/index.md
blob: 3b2f1d21463ea6c28b77f5abeab8496ca5826fd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
---
stage: Growth
group: Activation
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
---

# Experiment Guide

Experiments can be conducted by any GitLab team, most often the teams from the [Growth Sub-department](https://about.gitlab.com/handbook/engineering/development/growth/). Experiments are not tied to releases because they will primarily target GitLab.com.

Experiments will be run as an A/B test and will be behind a feature flag to turn the test on or off. Based on the data the experiment generates, the team will decide if the experiment had a positive impact and will be the new default or rolled back.

## Experiment tracking issue

Each experiment should have an [Experiment tracking](https://gitlab.com/groups/gitlab-org/-/issues?scope=all&utf8=%E2%9C%93&state=opened&label_name[]=growth%20experiment&search=%22Experiment+tracking%22) issue to track the experiment from roll-out through to cleanup/removal. Immediately after an experiment is deployed, the due date of the issue should be set (this depends on the experiment but can be up to a few weeks in the future).
After the deadline, the issue needs to be resolved and either:

- It was successful and the experiment will be the new default.
- It was not successful and all code related to the experiment will be removed.

In either case, an outcome of the experiment should be posted to the issue with the reasoning for the decision.

## Code reviews

Experiments' code quality can fail our standards for several reasons. These
reasons can include not being added to the codebase for a long time, or because
of fast iteration to retrieve data. However, having the experiment run (or not
run) shouldn't impact GitLab's availability. To avoid or identify issues,
experiments are initially deployed to a small number of users. Regardless,
experiments still need tests.

If, as a reviewer or maintainer, you find code that would usually fail review
but is acceptable for now, mention your concerns with a note that there's no
need to change the code. The author can then add a comment to this piece of code
and link to the issue that resolves the experiment. If the experiment is
successful and becomes part of the product, any follow up issues should be
addressed.

## How to create an A/B test

### Implement the experiment

1. Add the experiment to the `Gitlab::Experimentation::EXPERIMENTS` hash in [`experimentation.rb`](https://gitlab.com/gitlab-org/gitlab/blob/master/lib%2Fgitlab%2Fexperimentation.rb):

   ```ruby
   EXPERIMENTS = {
     other_experiment: {
       #...
     },
     # Add your experiment here:
     signup_flow: {
       environment: ::Gitlab.dev_env_or_com?, # Target environment, defaults to enabled for development and GitLab.com
       tracking_category: 'Growth::Activation::Experiment::SignUpFlow' # Used for providing the category when setting up tracking data
     }
   }.freeze
   ```

1. Use the experiment in the code.

   - Use this standard for the experiment in a controller:

      ```ruby
      class RegistrationController < ApplicationController
       def show
         # experiment_enabled?(:experiment_key) is also available in views and helpers
         if experiment_enabled?(:signup_flow)
           # render the experiment
         else
           # render the original version
         end
       end
      end
      ```

   - Make the experiment available to the frontend in a controller:

      ```ruby
      before_action do
        push_frontend_experiment(:signup_flow)
      end
      ```

      The above will check whether the experiment is enabled and push the result to the frontend.

      You can check the state of the feature flag in JavaScript:

      ```javascript
      import { isExperimentEnabled } from '~/experimentation';

      if ( isExperimentEnabled('signupFlow') ) {
        // ...
      }
      ```

   - It is also possible to run an experiment outside of the controller scope, for example in a worker:

      ```ruby
      class SomeWorker
        def perform
          # Check if the experiment is enabled at all (the percentage_of_time_value > 0)
          return unless Gitlab::Experimentation.enabled?(:experiment_key)

          # Since we cannot access cookies in a worker, we need to bucket models based on a unique, unchanging attribute instead.
          # Use the following method to check if the experiment is enabled for a certain attribute, for example a username or email address:
          if Gitlab::Experimentation.enabled_for_attribute?(:experiment_key, some_attribute)
            # execute experimental code
          else
            # execute control code
          end
        end
      end
      ```

### Implement the tracking events

To determine whether the experiment is a success or not, we must implement tracking events
to acquire data for analyzing. We can send events to Snowplow via either the backend or frontend.
Read the [product analytics guide](https://about.gitlab.com/handbook/product/product-analytics-guide/) for more details.

#### Track backend events

The framework provides the following helper method that is available in controllers:

```ruby
before_action do
  track_experiment_event(:signup_flow, 'action', 'value')
end
```

Which can be tested as follows:

```ruby
context 'when the experiment is active and the user is in the experimental group' do
  before do
    stub_experiment(signup_flow: true)
    stub_experiment_for_user(signup_flow: true)
  end

  it 'tracks an event', :snowplow do
    subject

    expect_snowplow_event(
      category: 'Growth::Activation::Experiment::SignUpFlow',
      action: 'action',
      value: 'value',
      label: 'experimentation_subject_id',
      property: 'experimental_group'
    )
  end
end
```

#### Track frontend events

The framework provides the following helper method that is available in controllers:

```ruby
before_action do
  push_frontend_experiment(:signup_flow)
  frontend_experimentation_tracking_data(:signup_flow, 'action', 'value')
end
```

This pushes tracking data to `gon.experiments` and `gon.tracking_data`.

```ruby
expect(Gon.experiments['signupFlow']).to eq(true)

expect(Gon.tracking_data).to eq(
  {
    category: 'Growth::Activation::Experiment::SignUpFlow',
    action: 'action',
    value: 'value',
    label: 'experimentation_subject_id',
    property: 'experimental_group'
  }
)
```

Which can then be used for tracking as follows:

```javascript
import { isExperimentEnabled } from '~/lib/utils/experimentation';
import Tracking from '~/tracking';

document.addEventListener('DOMContentLoaded', () => {
  const signupFlowExperimentEnabled = isExperimentEnabled('signupFlow');

  if (signupFlowExperimentEnabled && gon.tracking_data) {
    const { category, action, ...data } = gon.tracking_data;

    Tracking.event(category, action, data);
  }
}
```

Which can be tested in Jest as follows:

```javascript
import { withGonExperiment } from 'helpers/experimentation_helper';
import Tracking from '~/tracking';

describe('event tracking', () => {
  describe('with tracking data', () => {
    withGonExperiment('signupFlow');

    beforeEach(() => {
      jest.spyOn(Tracking, 'event').mockImplementation(() => {});

      gon.tracking_data = {
        category: 'Growth::Activation::Experiment::SignUpFlow',
        action: 'action',
        value: 'value',
        label: 'experimentation_subject_id',
        property: 'experimental_group'
      };
    });

    it('should track data', () => {
      performAction()

      expect(Tracking.event).toHaveBeenCalledWith(
        'Growth::Activation::Experiment::SignUpFlow',
        'action',
        {
          value: 'value',
          label: 'experimentation_subject_id',
          property: 'experimental_group'
        },
      );
    });
  });
});
```

### Enable the experiment

After all merge requests have been merged, use [`chatops`](../../ci/chatops/README.md) in the
[appropriate channel](../feature_flags/controls.md#communicate-the-change) to start the experiment for 10% of the users.
The feature flag should have the name of the experiment with the `_experiment_percentage` suffix appended.
For visibility, please also share any commands run against production in the `#s_growth` channel:

  ```shell
  /chatops run feature set signup_flow_experiment_percentage 10
  ```

  If you notice issues with the experiment, you can disable the experiment by removing the feature flag:

  ```shell
  /chatops run feature delete signup_flow_experiment_percentage
  ```

### Testing and test helpers

#### RSpec

Use the following in RSpec to mock the experiment:

```ruby
context 'when the experiment is active' do
  before do
    stub_experiment(signup_flow: true)
  end

  context 'when the user is in the experimental group' do
    before do
      stub_experiment_for_user(signup_flow: true)
    end

    it { is_expected.to do_experimental_thing }
  end

  context 'when the user is in the control group' do
    before do
      stub_experiment_for_user(signup_flow: false)
    end

    it { is_expected.to do_control_thing }
  end
end
```

#### Jest

Use the following in Jest to mock the experiment:

```javascript
import { withGonExperiment } from 'helpers/experimentation_helper';

describe('given experiment is enabled', () => {
  withGonExperiment('signupFlow');

  it('should do the experimental thing', () => {
    expect(wrapper.find('.js-some-experiment-triggered-element')).toEqual(expect.any(Element));
  });
});
```