Commit f579a78d authored by Sean McGivern's avatar Sean McGivern

Merge branch '13297-track-self-describing-event' into 'master'

Track self describing events

Closes #13297

See merge request gitlab-org/gitlab!18188
parents aff4a27c dc2454e3
...@@ -15,6 +15,10 @@ module Gitlab ...@@ -15,6 +15,10 @@ module Gitlab
category = args.delete(:category) || self.class.name category = args.delete(:category) || self.class.name
Gitlab::Tracking.event(category, action.to_s, **args) Gitlab::Tracking.event(category, action.to_s, **args)
end end
def track_self_describing_event(schema_url, event_data_json, **args)
Gitlab::Tracking.self_describing_event(schema_url, event_data_json, **args)
end
end end
class << self class << self
...@@ -28,6 +32,13 @@ module Gitlab ...@@ -28,6 +32,13 @@ module Gitlab
snowplow.track_struct_event(category, action, label, property, value, context, Time.now.to_i) snowplow.track_struct_event(category, action, label, property, value, context, Time.now.to_i)
end end
def self_describing_event(schema_url, event_data_json, context: nil)
return unless enabled?
event_json = SnowplowTracker::SelfDescribingJson.new(schema_url, event_data_json)
snowplow.track_self_describing_event(event_json, context, Time.now.to_i)
end
def snowplow_options(group) def snowplow_options(group)
additional_features = Feature.enabled?(:additional_snowplow_tracking, group) additional_features = Feature.enabled?(:additional_snowplow_tracking, group)
{ {
......
...@@ -12,10 +12,8 @@ describe Gitlab::Tracking do ...@@ -12,10 +12,8 @@ describe Gitlab::Tracking do
end end
describe '.snowplow_options' do describe '.snowplow_options' do
subject(&method(:described_class))
it 'returns useful client options' do it 'returns useful client options' do
expect(subject.snowplow_options(nil)).to eq( expect(described_class.snowplow_options(nil)).to eq(
namespace: 'gl', namespace: 'gl',
hostname: 'gitfoo.com', hostname: 'gitfoo.com',
cookieDomain: '.gitfoo.com', cookieDomain: '.gitfoo.com',
...@@ -32,23 +30,35 @@ describe Gitlab::Tracking do ...@@ -32,23 +30,35 @@ describe Gitlab::Tracking do
'_group_' '_group_'
).and_return(false) ).and_return(false)
expect(subject.snowplow_options('_group_')).to include( expect(described_class.snowplow_options('_group_')).to include(
formTracking: false, formTracking: false,
linkClickTracking: false linkClickTracking: false
) )
end end
end end
describe '.event' do describe 'tracking events' do
subject(&method(:described_class)) shared_examples 'events not tracked' do
it 'does not track events' do
stub_application_setting(snowplow_enabled: false)
expect(SnowplowTracker::AsyncEmitter).not_to receive(:new)
expect(SnowplowTracker::Tracker).not_to receive(:new)
track_event
end
end
around do |example| around do |example|
Timecop.freeze(timestamp) { example.run } Timecop.freeze(timestamp) { example.run }
end end
it 'can track events' do before do
tracker = double described_class.instance_variable_set("@snowplow", nil)
end
let(:tracker) { double }
def receive_events
expect(SnowplowTracker::AsyncEmitter).to receive(:new).with( expect(SnowplowTracker::AsyncEmitter).to receive(:new).with(
'gitfoo.com', { protocol: 'https' } 'gitfoo.com', { protocol: 'https' }
).and_return('_emitter_') ).and_return('_emitter_')
...@@ -59,30 +69,67 @@ describe Gitlab::Tracking do ...@@ -59,30 +69,67 @@ describe Gitlab::Tracking do
'gl', 'gl',
'_abc123_' '_abc123_'
).and_return(tracker) ).and_return(tracker)
end
describe '.event' do
let(:track_event) do
described_class.event('category', 'action',
label: '_label_',
property: '_property_',
value: '_value_',
context: nil
)
end
it_behaves_like 'events not tracked'
it 'can track events' do
receive_events
expect(tracker).to receive(:track_struct_event).with( expect(tracker).to receive(:track_struct_event).with(
'category', 'category',
'action', 'action',
'_label_', '_label_',
'_property_', '_property_',
'_value_', '_value_',
'_context_', nil,
timestamp.to_i timestamp.to_i
) )
subject.event('category', 'action', track_event
label: '_label_', end
property: '_property_', end
value: '_value_',
context: '_context_' describe '.self_describing_event' do
let(:track_event) do
described_class.self_describing_event('iglu:com.gitlab/example/jsonschema/1-0-2',
{
foo: 'bar',
foo_count: 42
},
context: nil
) )
end end
it 'does not track when not enabled' do it_behaves_like 'events not tracked'
stub_application_setting(snowplow_enabled: false)
expect(SnowplowTracker::Tracker).not_to receive(:new) it 'can track self describing events' do
receive_events
expect(SnowplowTracker::SelfDescribingJson).to receive(:new).with(
'iglu:com.gitlab/example/jsonschema/1-0-2',
{
foo: 'bar',
foo_count: 42
}
).and_return('_event_json_')
subject.event('epics', 'action', property: 'what', value: 'doit') expect(tracker).to receive(:track_self_describing_event).with(
'_event_json_',
nil,
timestamp.to_i
)
track_event
end
end end
end end
end end
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