Skip to content

Use new ExperimentSubject model with experimentation helper methods

This is a follow-up from #250800 (closed).

  • adjust the existing record_experiment_user(…) method to be record_experiment_subject(…)
  • we’ll assume that the type of subject is constant for a given experiment, so each row will be unique by the experiment_key and subject values (i.e. for record_experiment_subject(:exp_key, @group), each row in the experiment_subjects table would be considered unique by experiment_id & group_id).

Here’s an example that replaces some existing usage of record_experiment_user(…) with the proposed record_experiment_subject(…):

Before:

class TrialsController < ApplicationController
  def new
    record_experiment_user(:remove_known_trial_form_fields)
    record_experiment_user(:trimmed_skip_trial_copy)
  end
end

After:

class TrialsController < ApplicationController
  def new
    record_experiment_subject(:remove_known_trial_form_fields, current_user)
    record_experiment_subject(:trimmed_skip_trial_copy, current_user)
  end
end

Since recording the current_user as an experiment subject is fairly common, we’ll likely keep the record_experiment_user helper method, but replace its internals with record_experiment_subject(exp_key, current_user):

       def record_experiment_user(experiment_key)
-        return if dnt_enabled?
-        return unless Experimentation.active?(experiment_key) && current_user
-
-        ::Experiment.add_user(experiment_key, tracking_group(experiment_key, nil, subject: current_user), current_user)
+        record_experiment_subject(experiment_key, current_user)
+      end
+
+      def record_experiment_subject(experiment_key, subject)
+        return if dnt_enabled?
+        return unless Experimentation.active?(experiment_key) && subject
+
+        ::Experiment.add_subject(experiment_key, tracking_group(experiment_key, nil, subject: subject), subject)
       end
Edited by Dallas Reedy