summaryrefslogtreecommitdiff
path: root/spec/controllers/concerns/product_analytics_tracking_spec.rb
blob: 250cc3cf2cf6a7378c43766b80dbfbc11fd25b50 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

require "spec_helper"

RSpec.describe ProductAnalyticsTracking, :snowplow do
  include TrackingHelpers
  include SnowplowHelpers

  let(:user) { create(:user) }
  let!(:group) { create(:group) }

  before do
    allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
  end

  controller(ApplicationController) do
    include ProductAnalyticsTracking

    skip_before_action :authenticate_user!, only: :show
    track_event(:index, :show, name: 'g_analytics_valuestream', destinations: [:redis_hll, :snowplow],
                          conditions: [:custom_condition_one?, :custom_condition_two?]) { |controller| controller.get_custom_id }

    def index
      render html: 'index'
    end

    def new
      render html: 'new'
    end

    def show
      render html: 'show'
    end

    def get_custom_id
      'some_custom_id'
    end

    private

    def tracking_namespace_source
      Group.first
    end

    def custom_condition_one?
      true
    end

    def custom_condition_two?
      true
    end
  end

  def expect_tracking(user: self.user)
    expect(Gitlab::UsageDataCounters::HLLRedisCounter).to have_received(:track_event)
                                                            .with('g_analytics_valuestream', values: instance_of(String))

    expect_snowplow_event(
      category: anything,
      action: 'g_analytics_valuestream',
      namespace: group,
      user: user
    )
  end

  def expect_no_tracking
    expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)

    expect_no_snowplow_event
  end

  context 'when user is logged in' do
    before do
      sign_in(user)
    end

    it 'tracks the event' do
      get :index

      expect_tracking
    end

    context 'when FF is disabled' do
      before do
        stub_feature_flags(route_hll_to_snowplow: false)
      end

      it 'doesnt track snowplow event' do
        get :index

        expect_no_snowplow_event
      end
    end

    it 'tracks the event if DNT is not enabled' do
      stub_do_not_track('0')

      get :index

      expect_tracking
    end

    it 'does not track the event if DNT is enabled' do
      stub_do_not_track('1')

      get :index

      expect_no_tracking
    end

    it 'does not track the event if the format is not HTML' do
      get :index, format: :json

      expect_no_tracking
    end

    it 'does not track the event if a custom condition returns false' do
      allow(controller).to receive(:custom_condition_two?).and_return(false)

      get :index

      expect_no_tracking
    end

    it 'does not track the event for untracked actions' do
      get :new

      expect_no_tracking
    end
  end

  context 'when user is not logged in' do
    let(:visitor_id) { SecureRandom.uuid }

    it 'tracks the event when there is a visitor id' do
      cookies[:visitor_id] = { value: visitor_id, expires: 24.months }

      get :show, params: { id: 1 }

      expect_tracking(user: nil)
    end
  end

  context 'when user is not logged in and there is no visitor_id' do
    it 'does not track the event' do
      get :index

      expect_no_tracking
    end

    it 'tracks the event when there is custom id' do
      get :show, params: { id: 1 }

      expect_tracking(user: nil)
    end

    it 'does not track the HLL event when there is no custom id' do
      allow(controller).to receive(:get_custom_id).and_return(nil)

      get :show, params: { id: 2 }

      expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
      expect_snowplow_event(
        category: anything,
        action: 'g_analytics_valuestream',
        namespace: group,
        user: nil
      )
    end
  end
end