summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/tracking_spec.rb
blob: 2e65f98a0850f765ab6174cd70ae7662d645ef5f (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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_app_id: '_abc123_')
    stub_application_setting(snowplow_iglu_registry_url: 'https://example.org')
  end

  describe '.snowplow_options' do
    it 'returns useful client options' do
      expected_fields = {
        namespace: 'gl',
        hostname: 'gitfoo.com',
        cookieDomain: '.gitfoo.com',
        appId: '_abc123_',
        formTracking: true,
        linkClickTracking: true,
        igluRegistryUrl: 'https://example.org'
      }

      expect(subject.snowplow_options(nil)).to match(expected_fields)
    end

    it 'enables features using feature flags' do
      stub_feature_flags(additional_snowplow_tracking: :__group__)
      addition_feature_fields = {
        formTracking: false,
        linkClickTracking: false
      }

      expect(subject.snowplow_options(:_group_)).to include(addition_feature_fields)
    end
  end

  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

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

    before do
      described_class.instance_variable_set("@snowplow", nil)
    end

    let(:tracker) { double }

    def receive_events
      expect(SnowplowTracker::AsyncEmitter).to receive(:new).with(
        'gitfoo.com', { protocol: 'https' }
      ).and_return('_emitter_')

      expect(SnowplowTracker::Tracker).to receive(:new).with(
        '_emitter_',
        an_instance_of(SnowplowTracker::Subject),
        'gl',
        '_abc123_'
      ).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(
          'category',
          'action',
          '_label_',
          '_property_',
          '_value_',
          nil,
          (timestamp.to_f * 1000).to_i
        )

        track_event
      end
    end

    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,
          (timestamp.to_f * 1000).to_i
        )

        track_event
      end
    end
  end
end