summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/tracking_spec.rb
blob: f14e74427e115a731b84c3308885dd886a2345db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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')
    stub_application_setting(snowplow_site_id: '_abc123_')
  end

  describe '.snowplow_options' do
    subject(&method(:described_class))

    it 'returns useful client options' do
      expect(subject.snowplow_options(nil)).to eq(
        namespace: 'gl',
        hostname: 'gitfoo.com',
        cookieDomain: '.gitfoo.com',
        appId: '_abc123_',
        pageTrackingEnabled: true,
        activityTrackingEnabled: true
      )
    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)

      expect(subject.snowplow_options('_group_')).to include(
        pageTrackingEnabled: false,
        activityTrackingEnabled: false
      )
    end
  end

  describe '.event' do
    subject(&method(:described_class))

    around do |example|
      Timecop.freeze(timestamp) { example.run }
    end

    it 'can track events' do
      tracker = double

      expect(SnowplowTracker::Emitter).to receive(:new).with(
        'gitfoo.com'
      ).and_return('_emitter_')

      expect(SnowplowTracker::Tracker).to receive(:new).with(
        '_emitter_',
        an_instance_of(SnowplowTracker::Subject),
        'gl',
        '_abc123_'
      ).and_return(tracker)

      expect(tracker).to receive(:track_struct_event).with(
        'category',
        'action',
        '_label_',
        '_property_',
        '_value_',
        '_context_',
        timestamp.to_i
      )

      subject.event('category', 'action',
        label: '_label_',
        property: '_property_',
        value: '_value_',
        context: '_context_'
      )
    end

    it 'does not track when not enabled' do
      stub_application_setting(snowplow_enabled: false)
      expect(SnowplowTracker::Tracker).not_to receive(:new)

      subject.event('epics', 'action', property: 'what', value: 'doit')
    end
  end
end