2019-08-28 06:52:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Tracking do
|
|
|
|
let(:timestamp) { Time.utc(2017, 3, 22) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(snowplow_enabled: true)
|
|
|
|
stub_application_setting(snowplow_collector_hostname: 'gitfoo.com')
|
|
|
|
stub_application_setting(snowplow_cookie_domain: '.gitfoo.com')
|
2019-11-08 21:06:38 +00:00
|
|
|
stub_application_setting(snowplow_app_id: '_abc123_')
|
2019-10-31 21:06:28 +00:00
|
|
|
stub_application_setting(snowplow_iglu_registry_url: 'https://example.org')
|
2019-08-28 06:52:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.snowplow_options' do
|
|
|
|
it 'returns useful client options' do
|
2019-10-23 09:06:03 +00:00
|
|
|
expected_fields = {
|
2019-08-28 06:52:14 +00:00
|
|
|
namespace: 'gl',
|
|
|
|
hostname: 'gitfoo.com',
|
|
|
|
cookieDomain: '.gitfoo.com',
|
|
|
|
appId: '_abc123_',
|
2019-09-18 14:02:45 +00:00
|
|
|
formTracking: true,
|
2019-10-31 21:06:28 +00:00
|
|
|
linkClickTracking: true,
|
|
|
|
igluRegistryUrl: 'https://example.org'
|
2019-10-23 09:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 21:06:26 +00:00
|
|
|
expect(subject.snowplow_options(nil)).to match(expected_fields)
|
2019-08-28 06:52:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enables features using feature flags' do
|
|
|
|
stub_feature_flags(additional_snowplow_tracking: true)
|
|
|
|
allow(Feature).to receive(:enabled?).with(
|
|
|
|
:additional_snowplow_tracking,
|
|
|
|
'_group_'
|
|
|
|
).and_return(false)
|
2019-10-23 09:06:03 +00:00
|
|
|
addition_feature_fields = {
|
2019-09-18 14:02:45 +00:00
|
|
|
formTracking: false,
|
|
|
|
linkClickTracking: false
|
2019-10-23 09:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 21:06:26 +00:00
|
|
|
expect(subject.snowplow_options('_group_')).to include(addition_feature_fields)
|
2019-08-28 06:52:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-11 09:06:43 +00:00
|
|
|
describe 'tracking events' do
|
|
|
|
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
|
2019-08-28 06:52:14 +00:00
|
|
|
|
|
|
|
around do |example|
|
|
|
|
Timecop.freeze(timestamp) { example.run }
|
|
|
|
end
|
|
|
|
|
2019-10-11 09:06:43 +00:00
|
|
|
before do
|
|
|
|
described_class.instance_variable_set("@snowplow", nil)
|
|
|
|
end
|
2019-08-28 06:52:14 +00:00
|
|
|
|
2019-10-11 09:06:43 +00:00
|
|
|
let(:tracker) { double }
|
|
|
|
|
|
|
|
def receive_events
|
2019-09-24 09:06:04 +00:00
|
|
|
expect(SnowplowTracker::AsyncEmitter).to receive(:new).with(
|
|
|
|
'gitfoo.com', { protocol: 'https' }
|
2019-08-28 06:52:14 +00:00
|
|
|
).and_return('_emitter_')
|
|
|
|
|
|
|
|
expect(SnowplowTracker::Tracker).to receive(:new).with(
|
|
|
|
'_emitter_',
|
|
|
|
an_instance_of(SnowplowTracker::Subject),
|
|
|
|
'gl',
|
|
|
|
'_abc123_'
|
|
|
|
).and_return(tracker)
|
2019-10-11 09:06:43 +00:00
|
|
|
end
|
2019-08-28 06:52:14 +00:00
|
|
|
|
2019-10-11 09:06:43 +00:00
|
|
|
describe '.event' do
|
|
|
|
let(:track_event) do
|
|
|
|
described_class.event('category', 'action',
|
|
|
|
label: '_label_',
|
|
|
|
property: '_property_',
|
|
|
|
value: '_value_',
|
|
|
|
context: nil
|
|
|
|
)
|
|
|
|
end
|
2019-08-28 06:52:14 +00:00
|
|
|
|
2019-10-11 09:06:43 +00:00
|
|
|
it_behaves_like 'events not tracked'
|
|
|
|
|
|
|
|
it 'can track events' do
|
|
|
|
receive_events
|
|
|
|
expect(tracker).to receive(:track_struct_event).with(
|
|
|
|
'category',
|
|
|
|
'action',
|
|
|
|
'_label_',
|
|
|
|
'_property_',
|
|
|
|
'_value_',
|
|
|
|
nil,
|
2019-12-03 12:06:34 +00:00
|
|
|
(timestamp.to_f * 1000).to_i
|
2019-10-11 09:06:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
track_event
|
|
|
|
end
|
2019-08-28 06:52:14 +00:00
|
|
|
end
|
|
|
|
|
2019-10-11 09:06:43 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
it_behaves_like 'events not tracked'
|
|
|
|
|
|
|
|
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_')
|
|
|
|
|
|
|
|
expect(tracker).to receive(:track_self_describing_event).with(
|
|
|
|
'_event_json_',
|
|
|
|
nil,
|
2019-12-03 12:06:34 +00:00
|
|
|
(timestamp.to_f * 1000).to_i
|
2019-10-11 09:06:43 +00:00
|
|
|
)
|
2019-08-28 06:52:14 +00:00
|
|
|
|
2019-10-11 09:06:43 +00:00
|
|
|
track_event
|
|
|
|
end
|
2019-08-28 06:52:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|