gitlab-experiment 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b64d15c51bec74dc0b9be36e3b1541c3509c0f62a2259595c71366ff66f35e3
4
- data.tar.gz: 5c6862e1a9d44335ebe1d6b6e3974da4e0e368297d006eb91398ebf9d3004681
3
+ metadata.gz: 4bb028b4495b7ab210c13769433a8b6ec7b8a3fc459a2c33f0e9bc650cf23916
4
+ data.tar.gz: '0387ed9d7e722c4ba6bce42aed1b71720b6df23b6687100b02186f57b6f125c9'
5
5
  SHA512:
6
- metadata.gz: 0cea1580efdbe7daa88f76fb93de94d4ea04059bbc1bd25a15edf4a501e139ec9102c6abf9c2002e797ecfc9bb6ffc3115dcf4e817b94b5f6499ed09ca14de75
7
- data.tar.gz: 20490eec566060797379ecea83e07fe74a00f2725817bc396b6bea7cfc07947cd120562d433fb33d6db5b4094c58fd50b2ff1cb9455867c6300443fc4c2c6509
6
+ metadata.gz: d27ae9c243b447d994060bc2250ade8be78351312c01fa591e67441f913fd67f9e2caed1de77caac454b0a6b84702ff865e6a58c796c8e014ba2cf530dd5df9b
7
+ data.tar.gz: 2a3dcfef0dbfeb7e9e35d3ca56ede0bc7d1feeeafa9774a0f9184eeb89dfe49a49e6fbc93f594332cda1d8c87b374069a0ab1c053ab51183d8fdf47af4769845
data/README.md CHANGED
@@ -1,73 +1,82 @@
1
- GitLab Experiment
2
- =================
1
+ # GitLab Experiment
3
2
 
4
- Here at GitLab, experiments are run as A/B/n tests and are evaluated by the data the experiment generates. The team reviews the data, determines the variant that performed most effectively, and promotes that variant as the new default code path (or reverts back to the control).
3
+ <img alt="experiment" src="/uploads/60990b2dbf4c0406bbf8b7f998de2dea/experiment.png" align="right" width="40%">
5
4
 
6
- When we discuss the behavior of this gem, we'll use terms like experiment, control, candidate, and variant. It's worth defining these terms so they're more understood.
5
+ Here at GitLab, we run experiments as A/B/n tests and review the data the experiment generates. From that data, we determine the best performing variant and promote it as the new default code path. Or revert back to the control if no variant outperformed it.
6
+
7
+ This library provides a clean and elegant DSL to define, run, and track your GitLab experiment.
8
+
9
+ When we discuss the behavior of this gem, we'll use terms like experiment, context, control, candidate, and variant. It's worth defining these terms so they're more understood.
7
10
 
8
11
  - `experiment` is any deviation of code paths we want to run sometimes and not others.
12
+ - `context` is used to identify a consistent experience we'll provide in an experiment.
9
13
  - `control` is the default, or "original" code path.
10
- - `candidate` is used if there's one experimental code path.
14
+ - `candidate` defines that there's one experimental code path.
11
15
  - `variant(s)` is used when more than one experimental code path exists.
12
16
 
13
- Candidate and variant are referencing the same concept, but can simplify how we speak about the available code paths of a given experiment.
17
+ Candidate and variant are the same concept, but simplify how we speak about experimental paths.<br clear="all">
14
18
 
15
19
  ## Installation
16
20
 
17
- Add the gem to your Gemfile and then bundle install.
21
+ Add the gem to your Gemfile and then `bundle install`.
18
22
 
19
23
  ```ruby
20
24
  gem 'gitlab-experiment'
21
25
  ```
22
26
 
23
- If you're using Rails, you can install an initializer that provides basic configuration by running the install generator.
27
+ If you're using Rails, you can install the initializer. It provides basic configuration and documentation.
24
28
 
25
29
  ```shell
26
- $ rails generate gitlab-experiment:install
30
+ $ rails generate gitlab_experiment:install
27
31
  ```
28
32
 
29
33
  ## Implementing an experiment
30
34
 
31
- For the sake of our example, let's say we want to run an experiment for the interface we render for subscription cancellation.
35
+ For the sake of an example let's make one up. Let's run an experiment on what we render for disabling desktop notifications.
36
+
37
+ In our control (current world) we show a simple toggle interface labeled, "Notifications." In our experiment we want a "Turn on/off desktop notifications" button with a confirmation.
38
+
39
+ The behavior will be the same, but the interface will be different and may involve more or fewer steps.
40
+
41
+ Our hypothesis is that this will make the action more clear and will help in making a choice about if that's what the user really wants to do.
32
42
 
33
- In our control (current world) we show a simple toggle interface that reads "Auto-renew", and in our experiment candidate we want to show a "Cancel subscription" button with a confirmation dialog. Ultimately the behavior will be the same, but the interface will be considerably different and may involve more or less steps.
34
-
35
- We hypothesize that making it more clear what the action is will help people in making a choice about taking that action or not.
43
+ We'll name our experiment `notification_toggle`. This name is prefixed based on configuration. If you've set `config.name_prefix = 'gitlab'`, the experiment name would be `gitlab_notification_toggle` elsewhere.
36
44
 
37
- We'll name our experiment `subscription_cancellation`. It's important to understand this name may be prefixed based on your configuration, so if you've set `config.name_prefix = "gitlab"` it would be `gitlab_subscription_cancellation`.
45
+ When you implement an experiment you'll need to provide a name, and a context. The name can show up in tracking calls, and potentially other aspects. The context determines the variant assigned, and should be consistent between calls. We'll discuss migrating context in later examples.
38
46
 
39
- When you implement and run an experiment you'll need to provide the name you've given it, and a context hash. The context hash is used to determine which variant to provide, and is expected to be consistent between calls to your experiment, so a context hash key can be generated and cached -- which is used to consistently render the same experience given the same context.
47
+ A context "key" represents the unique id of a context. It allows us to give the same experience between different calls to the experiment and can be used in caching.
48
+
49
+ Now in our experiment we're going to render one of two views: the control will be our current view, and the candidate will be the new toggle button with a confirmation flow.
40
50
 
41
- In our experiment we're going to render one of two views. Control will be our current one, and candidate will be the new view with a cancel button and javascript confirm call.
42
-
43
51
  ```ruby
44
52
  class SubscriptionsController < ApplicationController
45
- include Gitlab::Experiment::Dsl
46
-
47
53
  def show
48
- experiment(:subscription_cancellation, user_id: user.id) do |e|
49
- e.use { render_toggle_button } # control
50
- e.try { render_cancel_button } # candidate
54
+ experiment(:notification_toggle, actor: user) do |e|
55
+ e.use { render_toggle } # control
56
+ e.try { render_button } # candidate
51
57
  end
52
58
  end
53
59
  end
54
60
  ```
55
61
 
56
- You can also provide different variant names if you want to. In our case we might want to include the confirmation dialog step or not, to see which one behaves better within our wider experiment.
62
+ You can define the experiment using simple control/candidate paths, or provide named variants.
63
+
64
+ Handling multi-variant experiments is up to the configuration you provide around resolving variants. But in our example we may want to try with and without the confirmation. We can run any number of variations in our experiments this way.
57
65
 
58
66
  ```ruby
59
- experiment(:subscription_cancellation, user_id: user.id) do |e|
60
- e.use { render_toggle_button } # control
61
- e.try(:variant_one) { render_cancel_button(confirmation: true) }
62
- e.try(:variant_two) { render_cancel_button(confirmation: false) }
67
+ experiment(:notification_toggle, actor: user) do |e|
68
+ e.use { render_toggle } # control
69
+ e.try(:variant_one) { render_button(confirmation: true) }
70
+ e.try(:variant_two) { render_button(confirmation: false) }
63
71
  end
64
72
  ```
65
73
 
66
- Later, and elsewhere in code you can use the same `experiment` call to track events on the experiment. You can use this consistent event concept to build out funnels from the data that's being tracked. The important detail here is to use the same context between your calls to `experiment`. If the context is the same, we're able to consistently track the event in a way that associates it to which variant is being presented.
74
+ Understanding how an experiment can change behavior is important in evaluating its performance.
75
+
76
+ To this end, we track events that are important by calling the same experiment elsewhere in code. By using the same context, you'll have consistent behavior and the ability to track events to it.
67
77
 
68
78
  ```ruby
69
- exp = experiment(:subscription_cancellation, user_id: user.id)
70
- exp.track('clicked_button')
79
+ experiment(:notification_toggle, actor: user).track(:clicked_button)
71
80
  ```
72
81
 
73
82
  <details>
@@ -76,14 +85,14 @@ exp.track('clicked_button')
76
85
  ### Class level interface using `.run`
77
86
 
78
87
  ```ruby
79
- exp = Gitlab::Experiment.run(:subscription_cancellation, user_id: user.id) do |e|
80
- # Context may be passed in the block, but must be finalized before calling
88
+ exp = Gitlab::Experiment.run(:notification_toggle, actor: user) do |e|
89
+ # Context may be passed in the block, but must be finalized before calling
81
90
  # run or track.
82
- e.context(project_id: project.id) # add the project id to the context
83
- e.variant(:candidate) # always run the candidate
91
+ e.context(project: project) # add the project to the context
92
+
84
93
  # Define the control and candidate variant.
85
- e.use { toggle_button_interface } # control
86
- e.try { cancel_button_interface } # candidate
94
+ e.use { render_toggle } # control
95
+ e.try { render_button } # candidate
87
96
  end
88
97
 
89
98
  # Track an event on the experiment we've defined.
@@ -93,16 +102,16 @@ exp.track(:clicked_button)
93
102
  ### Instance level interface
94
103
 
95
104
  ```ruby
96
- exp = Gitlab::Experiment.new(:subscription_cancellation, user_id: user.id)
97
- # Context may be passed in the block, but must be finalized before calling
98
- # run or track.
99
- exp.context(project_id: project.id) # add the project id to the context
105
+ exp = Gitlab::Experiment.new(:notification_toggle, actor: user)
106
+ # Additional context may be provided to the instance (exp) but must be
107
+ # finalized before calling run or track.
108
+ exp.context(project: project) # add the project id to the context
100
109
 
101
110
  # Define the control and candidate variant.
102
- exp.use { toggle_button_interface } # control
103
- exp.try { cancel_button_interface } # candidate
111
+ exp.use { render_toggle } # control
112
+ exp.try { render_button } # candidate
104
113
 
105
- # Run the experiment -- returning the result.
114
+ # Run the experiment, returning the result.
106
115
  exp.run
107
116
 
108
117
  # Track an event on the experiment we've defined.
@@ -112,25 +121,25 @@ exp.track(:clicked_button)
112
121
  </details>
113
122
 
114
123
  <details>
115
- <summary>You can define use custom classes...</summary>
124
+ <summary>You can define and use custom classes...</summary>
116
125
 
117
126
  ### Custom class
118
127
 
119
128
  ```ruby
120
- class CancellationExperiment < Gitlab::Experiment
129
+ class NotificationExperiment < Gitlab::Experiment
121
130
  def initialize(variant_name = nil, **context, &block)
122
- super(:subscription_cancellation, variant_name, **context, &block)
131
+ super(:notification_toggle, variant_name, **context, &block)
123
132
 
124
133
  # Define the control and candidate variant.
125
- use { toggle_button_interface } # control
126
- try { cancel_button_interface } # candidate
134
+ use { render_toggle } # control
135
+ try { render_button } # candidate
127
136
  end
128
137
  end
129
138
 
130
- exp = CancellationExperiment.new(user_id: user.id) do |e|
131
- # Context may be passed in the block, but must be finalized before calling
132
- # run or track.
133
- e.context(project_id: project.id) # add the project id to the context
139
+ exp = NotificationExperiment.new(actor: user) do |e|
140
+ # Context may be provided within the block or to the instance (exp) but must
141
+ # be finalized before calling run or track.
142
+ e.context(project: project) # add the project id to the context
134
143
  end
135
144
 
136
145
  # Run the experiment -- returning the result.
@@ -143,76 +152,203 @@ exp.track(:clicked_button)
143
152
  </details>
144
153
 
145
154
  <details>
146
- <summary>You can also hard specify the variant to use...</summary>
155
+ <summary>You can also specify the variant to use...</summary>
147
156
 
148
- ### Specifying which variant to use
157
+ ### Specifying variant
149
158
 
150
- This should generally be discouraged, as it can change the experience users have during rollout, and may confuse generating reports from the tracking calls. It is possible however, and may be useful if you understand the implications.
159
+ You can hardcode the variant if you want. It's important to know what this might do to your data during rollout, so use this with consideration.
151
160
 
152
161
  ```ruby
153
- experiment(:subscription_cancellation, :no_interface, user_id: user.id) do |e|
154
- e.use { toggle_button_interface } # control
155
- e.try { cancel_button_interface } # candidate
162
+ experiment(:notification_toggle, :no_interface, actor: user) do |e|
163
+ e.use { render_toggle } # control
164
+ e.try { render_button } # candidate
156
165
  e.try(:no_interface) { no_interface! } # variant
157
166
  end
158
167
  ```
159
168
 
160
- Or you can set the variant within the block -- potentially using some unique or different segmentation strategy that you've written specifically for the experiment at hand.
169
+ Or you can set the variant within the block. This allows using unique segmentation logic or variant resolution if you need it.
161
170
 
162
171
  ```ruby
163
- experiment(:subscription_cancellation, user_id: user.id) do |e|
172
+ experiment(:notification_toggle, actor: user) do |e|
173
+ # Variant selection must be done before calling run or track.
164
174
  e.variant(:no_interface) # set the variant
165
175
  # ...
166
176
  end
167
177
  ```
168
178
 
179
+ Or it can be specified in the call to run if you call it from within the block.
180
+
181
+ ```ruby
182
+ experiment(:notification_toggle, actor: user) do |e|
183
+ # ...
184
+ # Variant selection can be specified when calling run.
185
+ e.run(:no_interface)
186
+ end
187
+ ```
188
+
169
189
  </details>
170
190
 
171
- The `experiment` method, and the underlying `Gitlab::Experiment` instance is an implementation on top of [Scientist](https://github.com/github/scientist). Generally speaking you can use the DSL that Scientist defines, but for experiments we use `experiment` instead of `science`, and specify the variant on initialization (or via `#variant` and not in the call to `#run`. The interface is otherwise the same, even though not every aspect of Scientist makes sense for experiments.
191
+ ### Return value
192
+
193
+ By default the return value is a `Gitlab::Experiment` instance. In simple cases you may want only the results of the experiment though. You can call `run` within the block to get the return value of the assigned variant.
194
+
195
+ ```ruby
196
+ experiment(:notification_toggle) do |e|
197
+ e.use { 'A' }
198
+ e.try { 'B' }
199
+ e.run
200
+ end # => 'A'
201
+ ```
202
+
203
+ ### Including the DSL
204
+
205
+ By default, `Gitlab::Experiment` injects itself into the controller and view layers. This exposes the `experiment` method application wide in those layers.
206
+
207
+ Some experiments may extend outside of those layers, so you may want to include it elsewhere. For instance in a mailer, service object, background job, or similar.
208
+
209
+ Note: In a lot of these contexts you may not have a reference to the request (unless you pass it in, or provide access to it) which may be needed if you want to enable cookie behaviors and track that through to user conversion.
210
+
211
+ ```ruby
212
+ class WelcomeMailer < ApplicationMailer
213
+ include Gitlab::Experiment::Dsl # include the `experiment` method
214
+
215
+ def welcome
216
+ @user = params[:user]
217
+
218
+ ex = experiment(:project_suggestions, actor: @user) do |e|
219
+ e.use { 'welcome' }
220
+ e.try { 'welcome_with_project_suggestions' }
221
+ end
222
+
223
+ mail(to: @user.email, subject: 'Welcome!', template: ex.run)
224
+ end
225
+ end
226
+ ```
172
227
 
173
228
  ### Context migrations
174
229
 
175
- There are times when we may need to change something that we're providing in the context while an experiment is running, or even add or remove contexts. We make this possible by passing the migration data to the experiment.
230
+ There are times when we need to change context while an experiment is running. We make this possible by passing the migration data to the experiment.
231
+
232
+ Take for instance, that you might be using `version: 1` in your context currently. To migrate this to `version: 2`, provide the portion of the context you wish to change using a `migrated_with` option.
233
+
234
+ In providing the context migration data, we can resolve an experience and its events all the way back. This can also help in keeping our cache relevant.
235
+
236
+ ```ruby
237
+ # Migrate just the `:version` portion of the previous context, `{ actor: project, version: 1 }`:
238
+ experiment(:my_experiment, actor: project, version: 2, migrated_with: { version: 1 })
239
+ ```
240
+
241
+ You can add or remove context by providing a `migrated_from` option. This approach expects a full context replacement -- i.e. what it was before you added or removed the new context key.
176
242
 
177
- Take for instance, that you might be using `version: 1` in your context currently. If you want to migrate this to `version: 2`, you just need to provide the context that you want to change using a `migrated_with` option. In doing this, a given experience (variant rendered/events tracked) can be resolved back through any number of migrations, and can be cached/resolved by using the context key value that was already already generated.
243
+ If you wanted to introduce a `version` to your context, provide the full previous context.
178
244
 
179
245
  ```ruby
180
- experiment(:my_experiment, user_id: 42, version: 2, migrated_with: { version: 1 })
246
+ # Migrate the full context from `{ actor: project }` to `{ actor: project, version: 1 }`:
247
+ experiment(:my_experiment, actor: project, version: 1, migrated_from: { actor: project })
181
248
  ```
182
249
 
183
- If you're adding or removing a new a value from the context, you'll need to use `migrated_from`, which expects a full context replacement -- e.g. what it was before you added or removed the new context key. For instance, if you wanted to introduce the `version: 1` concept to your context, you would need to use something like the following.
184
-
250
+ This can impact an experience if you:
251
+
252
+ 1. haven't implemented the concept of migrations in your variant resolver
253
+ 1. haven't enabled a reasonable caching mechanism
254
+
255
+ ### When there isn't an actor (cookie fallback)
256
+
257
+ When there isn't an identifying key in the context (this is `actor` by default), we fall back to cookies to provide a consistent experience for the client viewing them.
258
+
259
+ Once we assign a certain variant to a context, we need to always provide the same experience. We achieve this by setting a cookie for the experiment in question, but only when needed.
260
+
261
+ This cookie is a temporary, randomized uuid and isn't associated with a user. When we can finally provide an actor, the context is auto migrated from the cookie to that actor.
262
+
263
+ To read and write cookies, we provide the `request` from within the controller and views. The cookie migration will happen automatically if the experiment is within those layers.
264
+
265
+ You'll need to provide the `request` as an option to the experiment if it's outside of the controller and views.
266
+
185
267
  ```ruby
186
- experiment(:my_experiment, user_id: 42, version: 1, migrated_from: { user_id: 42 })
268
+ experiment(:my_experiment, actor: user, request: request)
187
269
  ```
188
270
 
189
- It's important to understand that this can bucket a user in a new experience (depending on the logic in determining in the variant), so you should investigate how this might impact your experiment before using it and that your experiment is implemented in a way that supports migrations.
271
+ The cookie isn't set if the `actor` key isn't present at all in the context. Meaning that when no `actor` key is provided, the cookie will not be set.
272
+
273
+ ```ruby
274
+ # actor is not present, so no cookie is set
275
+ experiment(:my_experiment, project: project)
276
+
277
+ # actor is present and is nil, so the cookie is set and used
278
+ experiment(:my_experiment, actor: nil, project: project)
279
+
280
+ # actor is present and set to a value, so no cookie is set
281
+ experiment(:my_experiment, actor: user, project: project)
282
+ ```
283
+
284
+ For edge cases, you can pass the cookie through by assigning it yourself -- e.g. `actor: request.cookie_jar.signed['my_experiment_actor']`. The cookie name is the full experiment name (including any configured prefix) with `_actor` appended -- e.g. `gitlab_notification_toggle_actor` for the `:notification_toggle` experiment key with a configured prefix of `gitlab`.
285
+
286
+ ## Configuration
287
+
288
+ This gem needs to be configured before being used in a meaningful way.
289
+
290
+ The default configuration will always render the control, so it's important to configure your own logic for resolving variants.
291
+
292
+ Yes, the most important aspect of the gem -- that of determining which variant to render and when -- is up to you. Consider using [Unleash](https://github.com/Unleash/unleash-client-ruby) or [Flipper](https://github.com/jnunemaker/flipper) for this.
293
+
294
+ ```ruby
295
+ Gitlab::Experiment.configure do |config|
296
+ # The block here is evaluated within the scope of the experiment instance,
297
+ # which is why we are able to access things like name and context.
298
+ config.variant_resolver = lambda do |requested_variant|
299
+ # Return the requested variant if a specific one has been provided in code.
300
+ return requested_variant unless requested_variant.nil?
301
+
302
+ # Ask Unleash to determine the variant, given the context we've built,
303
+ # using the control as the fallback.
304
+ fallback = Unleash::Variant.new(name: 'control', enabled: true)
305
+ UNLEASH.get_variant(name, context.value, fallback)
306
+ end
307
+ end
308
+ ```
190
309
 
191
- ### When there isn't a user (cookies)
310
+ More examples for configuration are available in the provided [rails initializer](lib/generators/gitlab_experiment/install/templates/initializer.rb).
192
311
 
193
- When there isn't a user we typically have to fall back to another concept to provide a consistent experience. What this means, is that once we assign someone a certain variant, we want to always give them the same experience, and we do this by setting a cookie for the experiment we're currently checking on. This cookie value is a random uuid, which we auto migrate to a user_id when our experiment context is finally provided that information. This means that you really only need to provide the request as an option if you're wanting to have an experiment that flows from not-signed-in (or not registered) users to eventually signed-in users.
312
+ ### Client layer / JavaScript
194
313
 
195
- This is considered a temporary cookie value, and isn't used for tracking purposes other than to give a given "user" (in this case it's actually the browser), a consistent experience after we've assigned one.
314
+ This library doesn't attempt to provide any logic for the client layer.
196
315
 
197
- To read and write cookies, we allow for passing the `request` as an option. This allows us to read, write, and even clean up a cookie when appropriate. We've provided this by default in the ActionController interface though, so this is primarily useful if you're trying to have an experiment span controller and model layers.
316
+ Instead it allows you to do this yourself in configuration. Using [Gon](https://github.com/gazay/gon) to publish your experiment information to the client layer is pretty simple.
198
317
 
199
318
  ```ruby
200
- experiment(:subscription_cancellation, user_id: user.id, request: request) do |e|
201
- e.use { render_toggle_button } # control
202
- e.try { render_cancel_button } # candidate
319
+ Gitlab::Experiment.configure do |config|
320
+ config.publishing_behavior = lambda do |_result|
321
+ # Push the experiment knowledge into the front end. The signature contains
322
+ # the context key, and the variant that has been determined.
323
+ Gon.push({ experiment: { name => signature } }, true)
324
+ end
203
325
  end
204
- ```
326
+ ```
205
327
 
206
- If needed, for edge cases (like in a background job), you can manually pass in the cookie value. Passing it in as `user_id: request.cookie_jar.signed['subscription_cancellation_id']`. The cookie name is the experiment name (prefixed if configured to be) with `_id` appended.
328
+ In the client you can now access `window.gon.experiment.notificationToggle`.
207
329
 
208
- ## Configuration
330
+ ### Caching
209
331
 
210
- The gem is meant to be configured before being used. The default configuration will always render the control behavior, so it's important to implement your own logic for this or you will always get the control of an experiment.
332
+ Caching can be enabled in configuration, and is implemented towards the `Rails.cache` / `ActiveSupport::Cache::Store` interface. When you enable caching, any variant resolution will be cached. Migrating the cache through context migrations is handled automatically, and this helps ensure an experiment experience remains consistent.
211
333
 
212
- The most important aspect of the gem, determining which variant to render and when, is up to you, and you may want to consider using [Unleash](https://github.com/Unleash/unleash-client-ruby) (which has the concept of multi-variants built in), or [Flipper](https://github.com/jnunemaker/flipper) in helping with this.
334
+ It's important to understand that using caching can drastically change or override your rollout strategy logic.
213
335
 
214
- Examples for configuration are available in the provided install generator, or in the source code configuration.rb file itself.
336
+ ```ruby
337
+ Gitlab::Experiment.configure do |config|
338
+ config.cache = Rails.cache
339
+ end
340
+ ```
215
341
 
216
342
  ## Tracking, anonymity and GDPR
217
343
 
218
- We intentionally don't, and shouldn't, track things like user ids on our experiments. What we can and do track is what we consider an "experiment experience" key. This key is generated from the context we pass to the experiment and has the concept of migrating through different versions of context. If we consistently pass the same context to an experiment, we're able to consistently track events generated in that experience. A context can contain things like user, or project -- so, if you only included a user in the context that user would get the same experience across all projects they view, but if you include the currently viewed project in the context the user would potentially have a different experience on each of their projects. Each can be desirable given the objectives of the experiment.
344
+ We generally try not to track things like user identifying values in our experimentation. What we can and do track is the "experiment experience" (a.k.a. the context key).
345
+
346
+ We generate this key from the context passed to the experiment. This allows creating funnels without exposing any user information.
347
+
348
+ This library attempts to be non-user-centric, in that a context can contain things like a user or a project.
349
+
350
+ If you only include a user, that user would get the same experience across every project they view. If you only include the project, every user who views that project would get the same experience.
351
+
352
+ Each of these approaches could be desirable given the objectives of your experiment.
353
+
354
+ ### Make code not war
@@ -5,7 +5,10 @@ Gitlab::Experiment.configure do |config|
5
5
  config.name_prefix = nil
6
6
 
7
7
  # The logger is used to log various details of the experiments.
8
- config.logger = Logger.new(STDOUT)
8
+ config.logger = Logger.new($stdout)
9
+
10
+ # The caching layer is expected to respond to fetch, like Rails.cache.
11
+ config.cache = nil
9
12
 
10
13
  # Logic this project uses to resolve a variant for a given experiment.
11
14
  #
@@ -16,53 +19,60 @@ Gitlab::Experiment.configure do |config|
16
19
  # This block will be executed within the scope of the experiment instance,
17
20
  # so can easily access experiment methods, like getting the name or context.
18
21
  config.variant_resolver = lambda do |requested_variant|
19
- # An example of running the control, unless a variant was requested:
22
+ # Run the control, unless a variant was requested in code:
20
23
  requested_variant || 'control'
21
24
 
22
- # Always run candidate, unless a variant was requested, with fallback:
23
- # variant_name || variant_names.first || 'control'
25
+ # Run the candidate, unless a variant was requested, with a fallback:
26
+ #
27
+ # requested_variant || variant_names.first || 'control'
24
28
 
25
- # Using unleash to determine the variant:
26
- # TODO: this isn't entirely accurate.
29
+ # Using Unleash to determine the variant:
30
+ #
27
31
  # fallback = Unleash::Variant.new(name: requested_variant || 'control', enabled: true)
28
- # UNLEASH.get_variant(name, context.value, fallback)
32
+ # Unleash.get_variant(name, context.value, fallback)
29
33
 
30
34
  # Using Flipper to determine the variant:
35
+ #
31
36
  # TODO: provide example.
32
- # Variant.new(name: resolved)
37
+ # Variant.new(name: requested_variant || 'control')
33
38
  end
34
39
 
35
40
  # Tracking behavior can be implemented to link an event to an experiment.
36
41
  #
37
- # Similar to the variant resolver, this is called within the scope of the
38
- # experiment instance and so can access any methods on the experiment.
39
- config.tracking_behavior = lambda do |action, event_args|
42
+ # Similar to the variant_resolver, this is called within the scope of the
43
+ # experiment instance and so can access any methods on the experiment,
44
+ # such as name and signature.
45
+ config.tracking_behavior = lambda do |event, args|
40
46
  # An example of using a generic logger to track events:
41
- (event_args[:context] ||= []) << context.signature.merge(group: variant.name)
42
- config.logger.info "Gitlab::Experiment[#{name}] #{action}: #{event_args}"
47
+ config.logger.info "Gitlab::Experiment[#{name}] #{event}: #{args.merge(signature: signature)}"
43
48
 
44
- # Using something like snowplow to track events:
45
- # (event_args[:context] ||= []) << SnowplowTracker::SelfDescribingJson.new(
46
- # 'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0',
47
- # experiment: context.signature.merge(group: variant.name)
48
- # )
49
+ # Using something like snowplow to track events (in gitlab):
49
50
  #
50
- # Tracking.event(name, action, **event_args)
51
+ # Gitlab::Tracking.event(name, event, **args.merge(
52
+ # context: (args[:context] || []) << SnowplowTracker::SelfDescribingJson.new(
53
+ # 'iglu:com.gitlab/gitlab_experiment/jsonschema/0-2-0', signature
54
+ # )
55
+ # ))
51
56
  end
52
57
 
53
58
  # Called at the end of every experiment run, with the result.
54
59
  #
55
60
  # You may want to track that you've assigned a variant to a given context,
56
61
  # or push the experiment into the client or publish results elsewhere, like
57
- # into redis.
58
- config.publishing_behavior = lambda do |_result|
59
- track(:assignment) # this will call the config.tracking_behavior
62
+ # into redis. Also called within the scope of the experiment instance.
63
+ config.publishing_behavior = lambda do |result|
64
+ # Track the event using our own configured tracking logic.
65
+ track(:assignment)
60
66
 
61
- # Log the results, so we can inspect them if we wanted to later.
62
- # LogRage.log(result: _result)
67
+ # Push the experiment knowledge into the front end. The signature contains
68
+ # the context key, and the variant that has been determined.
69
+ #
70
+ # Gon.push({ experiment: { name => signature } }, true)
63
71
 
64
- # Push the experiment knowledge into the client using Gon.
65
- # Gon.push(experiment: { name => signature })
72
+ # Log using our logging system, so the result (which can be large) can be
73
+ # reviewed later if we want to.
74
+ #
75
+ # Lograge::Event.log(experiment: name, result: result, signature: signature)
66
76
  end
67
77
 
68
78
  # Algorithm that consistently generates a hash key for a given hash map.
@@ -2,7 +2,9 @@
2
2
 
3
3
  require 'scientist'
4
4
 
5
+ require 'gitlab/experiment/caching'
5
6
  require 'gitlab/experiment/configuration'
7
+ require 'gitlab/experiment/cookies'
6
8
  require 'gitlab/experiment/context'
7
9
  require 'gitlab/experiment/dsl'
8
10
  require 'gitlab/experiment/variant'
@@ -12,6 +14,7 @@ require 'gitlab/experiment/engine' if defined?(Rails::Engine)
12
14
  module Gitlab
13
15
  class Experiment
14
16
  include Scientist::Experiment
17
+ include Caching
15
18
 
16
19
  class << self
17
20
  def configure
@@ -19,23 +22,26 @@ module Gitlab
19
22
  end
20
23
 
21
24
  def run(name, variant_name = nil, **context, &block)
22
- instance = new(name, variant_name, **context, &block)
25
+ instance = constantize(name).new(name, variant_name, **context, &block)
23
26
  return instance unless block_given?
24
27
 
25
28
  instance.context.frozen? ? instance.run : instance.tap(&:run)
26
29
  end
27
- end
28
30
 
29
- delegate :signature, to: :context
31
+ def constantize(name)
32
+ name = "#{name}_experiment"
33
+ klass = name.respond_to?(:classify) ? name.classify.safe_constantize : nil
34
+ klass || self
35
+ end
36
+ end
30
37
 
31
38
  def initialize(name, variant_name = nil, **context)
32
39
  @name = name
33
40
  @variant_name = variant_name
34
- @context = Context.new(self)
41
+ @excluded = []
42
+ @context = Context.new(self, context)
35
43
 
36
- context(context)
37
-
38
- ignore { true }
44
+ exclude { !@context.trackable? }
39
45
  compare { false }
40
46
 
41
47
  yield self if block_given?
@@ -49,13 +55,23 @@ module Gitlab
49
55
  end
50
56
 
51
57
  def variant(value = nil)
52
- @variant_name = value unless value.nil?
58
+ return @variant_name = value unless value.nil?
59
+
53
60
  result = instance_exec(@variant_name, &Configuration.variant_resolver)
54
61
  result.respond_to?(:name) ? result : Variant.new(name: result.to_s)
55
62
  end
56
63
 
57
- def run
58
- @result ||= super(variant.name)
64
+ def exclude(&block)
65
+ @excluded << block
66
+ end
67
+
68
+ def run(variant_name = nil)
69
+ @result ||= begin
70
+ @variant_name = variant_name unless variant_name.nil?
71
+ @variant_name ||= :control if excluded?
72
+
73
+ super(cache { variant.name })
74
+ end
59
75
  end
60
76
 
61
77
  def publish(result)
@@ -63,6 +79,8 @@ module Gitlab
63
79
  end
64
80
 
65
81
  def track(action, **event_args)
82
+ return if excluded?
83
+
66
84
  instance_exec(action, event_args, &Configuration.tracking_behavior)
67
85
  end
68
86
 
@@ -71,13 +89,30 @@ module Gitlab
71
89
  end
72
90
 
73
91
  def variant_names
74
- @variant_names = behaviors.keys.tap { |keys| keys.delete('control') }.map(&:to_sym)
92
+ @variant_names ||= behaviors.keys.map(&:to_sym) - [:control]
93
+ end
94
+
95
+ def signature
96
+ { variant: variant.name, experiment: name }.merge(context.signature)
75
97
  end
76
98
 
77
99
  def enabled?
78
100
  true
79
101
  end
80
102
 
103
+ def excluded?
104
+ @excluded.any? { |exclude| exclude.call(self) }
105
+ end
106
+
107
+ def id
108
+ "#{name}:#{signature[:key]}"
109
+ end
110
+ alias_method :session_id, :id
111
+
112
+ def flipper_id
113
+ "Experiment;#{id}"
114
+ end
115
+
81
116
  protected
82
117
 
83
118
  def generate_result(variant_name)
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ class Experiment
5
+ module Caching
6
+ def cache(&block)
7
+ return yield unless (cache = Configuration.cache)
8
+
9
+ key, migrations = cache_strategy
10
+ migrated_cache(cache, migrations || [], key) or cache.fetch(key, &block)
11
+ end
12
+
13
+ private
14
+
15
+ def cache_strategy
16
+ [
17
+ "#{name}:#{signature[:key]}",
18
+ signature[:migration_keys]&.map { |key| "#{name}:#{key}" }
19
+ ]
20
+ end
21
+
22
+ def migrated_cache(cache, migrations, new_key)
23
+ migrations.find do |old_key|
24
+ next unless (value = cache.read(old_key))
25
+
26
+ cache.write(new_key, value)
27
+ cache.delete(old_key)
28
+ break value
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -13,7 +13,10 @@ module Gitlab
13
13
  @name_prefix = nil
14
14
 
15
15
  # The logger is used to log various details of the experiments.
16
- @logger = Logger.new(STDOUT)
16
+ @logger = Logger.new($stdout)
17
+
18
+ # Cache layer. Expected to respond to fetch, like Rails.cache.
19
+ @cache = nil
17
20
 
18
21
  # Logic this project uses to resolve a variant for a given experiment.
19
22
  @variant_resolver = lambda do |requested_variant|
@@ -21,14 +24,14 @@ module Gitlab
21
24
  end
22
25
 
23
26
  # Tracking behavior can be implemented to link an event to an experiment.
24
- @tracking_behavior = lambda do |action, event_args|
25
- Configuration.logger.info "Gitlab::Experiment[#{name}] #{action}: #{event_args.merge(signature: signature)}"
27
+ @tracking_behavior = lambda do |event, args|
28
+ Configuration.logger.info "Gitlab::Experiment[#{name}] #{event}: #{args.merge(signature: signature)}"
26
29
  end
27
30
 
28
31
  # Called at the end of every experiment run, with the results. You may
29
32
  # want to push the experiment into the client or push results elsewhere.
30
33
  @publishing_behavior = lambda do |_result|
31
- track(:assignment) # this will call the config.tracking_behavior
34
+ track(:assignment)
32
35
  end
33
36
 
34
37
  # Algorithm that consistently generates a hash key for a given hash map.
@@ -38,8 +41,15 @@ module Gitlab
38
41
  end
39
42
 
40
43
  class << self
41
- attr_accessor :name_prefix, :logger
42
- attr_accessor :variant_resolver, :tracking_behavior, :publishing_behavior, :context_hash_strategy
44
+ attr_accessor(
45
+ :name_prefix,
46
+ :logger,
47
+ :cache,
48
+ :variant_resolver,
49
+ :tracking_behavior,
50
+ :publishing_behavior,
51
+ :context_hash_strategy
52
+ )
43
53
  end
44
54
  end
45
55
  end
@@ -3,78 +3,65 @@
3
3
  module Gitlab
4
4
  class Experiment
5
5
  class Context
6
+ include Cookies
7
+
6
8
  DNT_REGEXP = /^(true|t|yes|y|1|on)$/i.freeze
7
9
 
8
- def initialize(experiment)
10
+ def initialize(experiment, **initial_value)
9
11
  @experiment = experiment
10
12
  @value = {}
11
- @migrations_from = []
12
- @migrations_with = []
13
+ @migrations = { merged: [], unmerged: [] }
14
+
15
+ value(initial_value)
16
+ end
17
+
18
+ def reinitialize(request)
19
+ @signature = nil # clear memoization
20
+ @request = request if request.respond_to?(:headers) && request.respond_to?(:cookie_jar)
13
21
  end
14
22
 
15
23
  def value(value = nil)
16
24
  return @value if value.nil?
17
25
 
18
26
  value = value.dup # dup so we don't mutate
19
- @signature = nil # clear memoized signature
27
+ reinitialize(value.delete(:request))
28
+
29
+ @value.merge!(process_migrations(value))
30
+ end
20
31
 
21
- @migrations_from << value.delete(:migrated_from) if value[:migrated_from]
22
- @migrations_with << value.delete(:migrated_with) if value[:migrated_with]
23
- @value.merge!(migrate_cookie_to_user_id(value))
32
+ def trackable?
33
+ !(@request && @request.headers['DNT'].to_s.match?(DNT_REGEXP))
24
34
  end
25
35
 
26
36
  def freeze
27
- signature # ensure we're done before being frozen
37
+ signature # finalize before freezing
28
38
  super
29
39
  end
30
40
 
31
41
  def signature
32
- @signature ||= {
33
- key: key_for(@value),
34
- migration_keys: migration_keys,
35
- variant: @experiment.variant.name
36
- }.compact
42
+ @signature ||= { key: key_for(@value), migration_keys: migration_keys }.compact
37
43
  end
38
44
 
39
45
  private
40
46
 
41
- def migrate_cookie_to_user_id(hash)
42
- return hash unless (request = hash.delete(:request))
43
- return hash unless request.respond_to?(:headers) && request.respond_to?(:cookie_jar)
44
- return hash if request.headers['DNT'].to_s.match?(DNT_REGEXP)
45
-
46
- jar = request.cookie_jar
47
- resolver = [jar, hash, :user_id, jar.signed[cookie_name]].compact
48
- resolve_cookie(*resolver) or generate_cookie(*resolver)
49
- end
50
-
51
- def cookie_name
52
- @cookie_name ||= [@experiment.name, 'id'].join('_')
53
- end
54
-
55
- def resolve_cookie(jar, hash, key, cookie = nil)
56
- return if cookie.blank? && hash[key].blank?
57
- return hash.merge(key => cookie) if hash[key].blank?
58
-
59
- @migrations_with << { user_id: cookie }
60
- jar.delete(cookie_name, domain: :all)
47
+ def process_migrations(value)
48
+ add_migration(value.delete(:migrated_from))
49
+ add_migration(value.delete(:migrated_with), merge: true)
61
50
 
62
- hash
51
+ migrate_cookie(value, "#{@experiment.name}_id")
63
52
  end
64
53
 
65
- def generate_cookie(jar, hash, key, cookie = SecureRandom.uuid)
66
- jar.permanent.signed[cookie_name] = {
67
- value: cookie, secure: true, domain: :all, httponly: true
68
- }
54
+ def add_migration(value, merge: false)
55
+ return unless value.is_a?(Hash)
69
56
 
70
- hash.merge(key => cookie)
57
+ @migrations[merge ? :merged : :unmerged] << value
71
58
  end
72
59
 
73
60
  def migration_keys
74
- return nil if @migrations_from.empty? && @migrations_with.empty?
61
+ return nil if @migrations[:unmerged].empty? && @migrations[:merged].empty?
75
62
 
76
- @migrations_from.map { |m| key_for(m) } +
77
- @migrations_with.map { |m| key_for(@value.merge(m)) }
63
+ @migrations[:unmerged].map { |m| key_for(m) } +
64
+ @migrations[:merged].map { |m| key_for(@value.merge(m)) }
78
65
  end
79
66
 
80
67
  def key_for(context)
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Gitlab
6
+ class Experiment
7
+ module Cookies
8
+ private
9
+
10
+ def migrate_cookie(hash, cookie_name)
11
+ return hash if cookie_jar.nil?
12
+
13
+ resolver = [hash, :actor, cookie_name, cookie_jar.signed[cookie_name]]
14
+ resolve_cookie(*resolver) or generate_cookie(*resolver)
15
+ end
16
+
17
+ def cookie_jar
18
+ @request&.cookie_jar
19
+ end
20
+
21
+ def resolve_cookie(hash, key, cookie_name, cookie)
22
+ return if cookie.to_s.empty? && hash[key].nil?
23
+ return hash if cookie.to_s.empty?
24
+ return hash.merge(key => cookie) if hash[key].nil?
25
+
26
+ add_migration(key => cookie)
27
+ cookie_jar.delete(cookie_name, domain: :all)
28
+
29
+ hash
30
+ end
31
+
32
+ def generate_cookie(hash, key, cookie_name, cookie)
33
+ return hash unless hash.key?(key)
34
+
35
+ cookie ||= SecureRandom.uuid
36
+ cookie_jar.permanent.signed[cookie_name] = {
37
+ value: cookie, secure: true, domain: :all, httponly: true
38
+ }
39
+
40
+ hash.merge(key => cookie)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -3,9 +3,13 @@
3
3
  module Gitlab
4
4
  class Experiment
5
5
  class Engine < ::Rails::Engine
6
+ def self.include_dsl
7
+ ActionController::Base.include(Dsl)
8
+ ActionController::Base.helper_method(:experiment)
9
+ end
10
+
6
11
  config.after_initialize do
7
- # Add out experiment method to the base controller.
8
- ActionController::Base.include(Dsl) if defined?(ActionController)
12
+ include_dsl if defined?(ActionController)
9
13
  end
10
14
  end
11
15
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gitlab
4
4
  class Experiment
5
- VERSION = '0.2.1'
5
+ VERSION = '0.3.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,35 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-experiment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitLab
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-24 00:00:00.000000000 Z
11
+ date: 2020-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: scientist
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 1.5.0
20
17
  - - "~>"
21
18
  - !ruby/object:Gem::Version
22
19
  version: '1.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: 1.5.0
30
27
  - - "~>"
31
28
  - !ruby/object:Gem::Version
32
29
  version: '1.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.0
33
33
  description:
34
34
  email:
35
35
  - gitlab_rubygems@gitlab.com
@@ -43,8 +43,10 @@ files:
43
43
  - lib/generators/gitlab_experiment/install/install_generator.rb
44
44
  - lib/generators/gitlab_experiment/install/templates/initializer.rb
45
45
  - lib/gitlab/experiment.rb
46
+ - lib/gitlab/experiment/caching.rb
46
47
  - lib/gitlab/experiment/configuration.rb
47
48
  - lib/gitlab/experiment/context.rb
49
+ - lib/gitlab/experiment/cookies.rb
48
50
  - lib/gitlab/experiment/dsl.rb
49
51
  - lib/gitlab/experiment/engine.rb
50
52
  - lib/gitlab/experiment/variant.rb
@@ -68,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
70
  - !ruby/object:Gem::Version
69
71
  version: '0'
70
72
  requirements: []
71
- rubygems_version: 3.0.3
73
+ rubygems_version: 3.1.4
72
74
  signing_key:
73
75
  specification_version: 4
74
76
  summary: GitLab experiment library built on top of scientist.