Commit 55822d83 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'jejacks0n/cleanup/move-tracking-behavior-to-configuration' into 'master'

Move experiment tracking behavior to configuration

See merge request gitlab-org/gitlab!65179
parents 87f8312b f51999c7
......@@ -32,17 +32,6 @@ class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/Namesp
Experiment.add_subject(name, variant: variant || :control, subject: subject)
end
def track(action, **event_args)
return unless should_track? # don't track events for excluded contexts
# track the event, and mix in the experiment signature data
Gitlab::Tracking.event(name, action.to_s, **event_args.merge(
context: (event_args[:context] || []) << SnowplowTracker::SelfDescribingJson.new(
'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0', signature
)
))
end
def record!
@record = true
end
......
......@@ -6,4 +6,11 @@ Gitlab::Experiment.configure do |config|
config.cache = Gitlab::Experiment::Cache::RedisHashStore.new(
pool: ->(&block) { Gitlab::Redis::SharedState.with { |redis| block.call(redis) } }
)
config.tracking_behavior = lambda do |action, event_args|
Gitlab::Tracking.event(name, action.to_s, **event_args.merge(
context: (event_args[:context] || []) << SnowplowTracker::SelfDescribingJson.new(
'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0', signature
)
))
end
end
......@@ -150,6 +150,10 @@ RSpec.describe ApplicationExperiment, :experiment do
end
describe "#track", :snowplow do
let(:fake_context) do
SnowplowTracker::SelfDescribingJson.new('iglu:com.gitlab/fake/jsonschema/0-0-0', { data: '_data_' })
end
it "doesn't track if we shouldn't track" do
allow(subject).to receive(:should_track?).and_return(false)
......@@ -159,9 +163,7 @@ RSpec.describe ApplicationExperiment, :experiment do
end
it "tracks the event with the expected arguments and merged contexts" do
subject.track(:action, property: '_property_', context: [
SnowplowTracker::SelfDescribingJson.new('iglu:com.gitlab/fake/jsonschema/0-0-0', { data: '_data_' })
])
subject.track(:action, property: '_property_', context: [fake_context])
expect_snowplow_event(
category: 'namespaced/stub',
......@@ -179,6 +181,26 @@ RSpec.describe ApplicationExperiment, :experiment do
]
)
end
it "tracks the event correctly even when using the base class" do
subject = Gitlab::Experiment.new(:unnamed)
subject.track(:action, context: [fake_context])
expect_snowplow_event(
category: 'unnamed',
action: 'action',
context: [
{
schema: 'iglu:com.gitlab/fake/jsonschema/0-0-0',
data: { data: '_data_' }
},
{
schema: 'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0',
data: { experiment: 'unnamed', key: subject.context.key, variant: 'control' }
}
]
)
end
end
describe "#key_for" do
......
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