Commit caddeff1 authored by jejacks0n's avatar jejacks0n Committed by Doug Stull

Utilize GSC for experiments when enabled

- This passes through known experiment context keys to the track call so
they can be utilized by the gitlab standard context if supported.
parent c827d97b
...@@ -41,6 +41,10 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp ...@@ -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 # define a default nil control behavior so we can omit it when not needed
end end
def track(action, **event_args)
super(action, **tracking_context.merge(event_args))
end
# TODO: remove # TODO: remove
# This is deprecated logic as of v0.6.0 and should eventually be removed, but # 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 # needs to stay intact for actively running experiments. The new strategy
...@@ -60,6 +64,19 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp ...@@ -60,6 +64,19 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
private 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 def feature_flag_name
name.tr('/', '_') name.tr('/', '_')
end end
......
...@@ -233,6 +233,75 @@ RSpec.describe ApplicationExperiment, :experiment do ...@@ -233,6 +233,75 @@ RSpec.describe ApplicationExperiment, :experiment do
] ]
) )
end 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 end
describe "#key_for" do describe "#key_for" do
......
...@@ -48,12 +48,16 @@ module SnowplowHelpers ...@@ -48,12 +48,16 @@ module SnowplowHelpers
# ) # )
def expect_snowplow_event(category:, action:, context: nil, **kwargs) def expect_snowplow_event(category:, action:, context: nil, **kwargs)
if context if context
if context.is_a?(Array)
kwargs[:context] = [] kwargs[:context] = []
context.each do |c| context.each do |c|
expect(SnowplowTracker::SelfDescribingJson).to have_received(:new) expect(SnowplowTracker::SelfDescribingJson).to have_received(:new)
.with(c[:schema], c[:data]).at_least(:once) .with(c[:schema], c[:data]).at_least(:once)
kwargs[:context] << an_instance_of(SnowplowTracker::SelfDescribingJson) kwargs[:context] << an_instance_of(SnowplowTracker::SelfDescribingJson)
end end
else
kwargs[:context] = context
end
end end
expect(Gitlab::Tracking).to have_received(:event) # rubocop:disable RSpec/ExpectGitlabTracking 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