Commit e03e1baa authored by Doug Stull's avatar Doug Stull

Merge branch 'jejacks0n/experiments-use-standard-gitlab-context' into 'master'

Utilize GSC for experiments when enabled

See merge request gitlab-org/gitlab!75146
parents a5d52308 caddeff1
......@@ -41,6 +41,10 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
# define a default nil control behavior so we can omit it when not needed
end
def track(action, **event_args)
super(action, **tracking_context.merge(event_args))
end
# TODO: remove
# This is deprecated logic as of v0.6.0 and should eventually be removed, but
# needs to stay intact for actively running experiments. The new strategy
......@@ -60,6 +64,19 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
private
def tracking_context
{
namespace: context.try(:namespace) || context.try(:group),
project: context.try(:project),
user: user_or_actor
}.compact || {}
end
def user_or_actor
actor = context.try(:actor)
actor.respond_to?(:id) ? actor : context.try(:user)
end
def feature_flag_name
name.tr('/', '_')
end
......
......@@ -233,6 +233,75 @@ RSpec.describe ApplicationExperiment, :experiment do
]
)
end
context "when using known context resources" do
let(:user) { build(:user, id: non_existing_record_id) }
let(:project) { build(:project, id: non_existing_record_id) }
let(:namespace) { build(:namespace, id: non_existing_record_id) }
let(:group) { build(:group, id: non_existing_record_id) }
let(:actor) { user }
let(:context) { { user: user, project: project, namespace: namespace } }
it "includes those using the gitlab standard context" do
subject.track(:action)
expect_snowplow_event(
category: 'namespaced/stub',
action: 'action',
user: user,
project: project,
namespace: namespace,
context: an_instance_of(Array)
)
end
it "falls back to using the group key" do
subject.context(namespace: nil, group: group)
subject.track(:action)
expect_snowplow_event(
category: 'namespaced/stub',
action: 'action',
user: user,
project: project,
namespace: group,
context: an_instance_of(Array)
)
end
context "with the actor key" do
it "provides it to the tracking call as the user" do
subject.context(user: nil, actor: actor)
subject.track(:action)
expect_snowplow_event(
category: 'namespaced/stub',
action: 'action',
user: actor,
project: project,
namespace: namespace,
context: an_instance_of(Array)
)
end
it "handles when it's not a user record" do
subject.context(user: nil, actor: nil)
subject.track(:action)
expect_snowplow_event(
category: 'namespaced/stub',
action: 'action',
project: project,
namespace: namespace,
context: an_instance_of(Array)
)
end
end
end
end
describe "#key_for" do
......
......@@ -48,12 +48,16 @@ module SnowplowHelpers
# )
def expect_snowplow_event(category:, action:, context: nil, **kwargs)
if context
if context.is_a?(Array)
kwargs[:context] = []
context.each do |c|
expect(SnowplowTracker::SelfDescribingJson).to have_received(:new)
.with(c[:schema], c[:data]).at_least(:once)
kwargs[:context] << an_instance_of(SnowplowTracker::SelfDescribingJson)
end
else
kwargs[:context] = context
end
end
expect(Gitlab::Tracking).to have_received(:event) # rubocop:disable RSpec/ExpectGitlabTracking
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment