summaryrefslogtreecommitdiff
path: root/doc/development/experiment_guide/experimentation.md
blob: 7135f8acd9b4d93fe463f5649545fb4a5f678b02 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
---
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/#assignments
---

# Create an A/B test with `Experimentation Module`

## 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: {
       tracking_category: 'Growth::Activation::Experiment::SignUpFlow' # Used for providing the category when setting up tracking data
     }
   }.freeze
   ```

1. Use the experiment in the code.

   Experiments can be performed on a `subject`. The provided `subject` should
   respond to `to_global_id` or `to_s`.
   The resulting string is bucketed and assigned to either the control or the
   experimental group, so you must always provide the same `subject`
   for an experiment to have the same experience.

   1. Use this standard for the experiment in a controller:

      - Experiment run for a user:

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

      - Experiment run for a namespace:

        ```ruby
        if experiment_enabled?(:signup_flow, subject: namespace)
          # experiment code
        else
          # control code
        end
        ```

      When no subject is given, it falls back to a cookie that gets set and is consistent until
      the cookie gets deleted.

      ```ruby
      class RegistrationController < ApplicationController
        def show
          # falls back to a cookie
          if experiment_enabled?(:signup_flow)
            # render the experiment
          else
            # render the original version
          end
        end
      end
      ```

   1. Make the experiment available to the frontend in a controller. This example
      checks whether the experiment is enabled and pushes the result to the frontend:

     ```ruby
     before_action do
       push_frontend_experiment(:signup_flow, subject: current_user)
     end
     ```

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

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

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

You can also run an experiment outside of the controller scope, such as in a worker:

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

    # Since we cannot access cookies in a worker, we need to bucket models
    # based on a unique, unchanging attribute instead.
    # It is therefore necessery to always provide the same subject.
    if Gitlab::Experimentation.in_experiment_group?(:experiment_key, subject: user)
      # execute experimental code
    else
      # execute control code
    end
  end
end
```

## Implement 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 intelligence guide](https://about.gitlab.com/handbook/product/product-intelligence-guide/) for more details.

### Track backend events

The framework provides a helper method that is available in controllers:

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

To test it:

```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_subject(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 a helper method that is available in controllers:

```ruby
before_action do
  push_frontend_experiment(:signup_flow, subject: current_user)
  frontend_experimentation_tracking_data(:signup_flow, 'action', 'value', subject: current_user)
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'
  }
)
```

To track it:

```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);
  }
}
```

To test it in Jest:

```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'
        },
      );
    });
  });
});
```

## Record experiment user

In addition to the anonymous tracking of events, we can also record which users
have participated in which experiments, and whether they were given the control
experience or the experimental experience.

The `record_experiment_user` helper method is available to all controllers, and it
enables you to record these experiment participants (the current user) and which
experience they were given:

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

Subsequent calls to this method for the same experiment and the same user have no
effect unless the user is then enrolled into a different experience. This happens
when we roll out the experimental experience to a greater percentage of users.

This data is completely separate from the [events tracking data](#implement-tracking-events).
They are not linked together in any way.

### Add context

You can add arbitrary context data in a hash which gets stored as part of the experiment
user record. New calls to the `record_experiment_user` with newer contexts are merged
deeply into the existing context.

This data can then be used by data analytics dashboards.

```ruby
before_action do
  record_experiment_user(:signup_flow, foo: 42, bar: { a: 22})
  # context is { "foo" => 42, "bar" => { "a" => 22 }}
end

# Additional contexts for newer record calls are merged deeply
record_experiment_user(:signup_flow, foo: 40, bar: { b: 2 }, thor: 3)
# context becomes { "foo" => 40, "bar" => { "a" => 22, "b" => 2 }, "thor" => 3}
```

## Record experiment conversion event

Along with the tracking of backend and frontend events and the
[recording of experiment participants](#record-experiment-user), we can also record
when a user performs the desired conversion event action. For example:

- **Experimental experience:** Show an in-product nudge to test if the change causes more
  people to sign up for trials.
- **Conversion event:** The user starts a trial.

The `record_experiment_conversion_event` helper method is available to all controllers.
Use it to record the conversion event for the current user, regardless of whether
the user is in the control or experimental group:

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

Note that the use of this method requires that we have first
[recorded the user](#record-experiment-user) as being part of the experiment.

## Enable the experiment

After all merge requests have been merged, use [ChatOps](../../ci/chatops/index.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, 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
  ```

## Add user to experiment group manually

To force the application to add your current user into the experiment group,
add a query string parameter to the path where the experiment runs. If you add the
query string parameter, the experiment works only for this request, and doesn't work
after following links or submitting forms.

For example, to forcibly enable the `EXPERIMENT_KEY` experiment, add `force_experiment=EXPERIMENT_KEY`
to the URL:

```shell
https://gitlab.com/<EXPERIMENT_ENTRY_URL>?force_experiment=<EXPERIMENT_KEY>
```

## Add user to experiment group with a cookie

You can force the current user into the experiment group for `<EXPERIMENT_KEY>`
during the browser session by using your browser's developer tools:

```javascript
document.cookie = "force_experiment=<EXPERIMENT_KEY>; path=/";
```

Use a comma to list more than one experiment to be forced:

```javascript
document.cookie = "force_experiment=<EXPERIMENT_KEY>,<ANOTHER_EXPERIMENT_KEY>; path=/";
```

To clear the experiments, unset the `force_experiment` cookie:

```javascript
document.cookie = "force_experiment=; path=/";
```

## 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_subject(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_subject(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));
  });
});
```