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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
# frozen_string_literal: true
require "spec_helper"
RSpec.describe ProductAnalyticsTracking, :snowplow, feature_category: :product_analytics do
include TrackingHelpers
include SnowplowHelpers
let(:user) { create(:user) }
let(:event_name) { 'an_event' }
let(:event_action) { 'an_action' }
let(:event_label) { 'a_label' }
let!(:group) { create(:group) }
let_it_be(:project) { create(:project) }
subject(:track_internal_event) { get :show, params: { id: 1 } }
describe '.track_internal_event' do
controller(ApplicationController) do
include ProductAnalyticsTracking
skip_before_action :authenticate_user!, only: [:index]
track_internal_event :index, :show, name: 'an_event', conditions: [:custom_condition?]
def index
render html: 'index'
end
def show
render html: 'show'
end
private
def tracking_namespace_source
tracking_project_source.namespace
end
def tracking_project_source
Project.first
end
def custom_condition?
true
end
end
def expect_internal_tracking(tracked_user: user)
expect(Gitlab::InternalEvents).to receive(:track_event).with(event_name,
user: tracked_user,
project: project,
namespace: project.namespace).once
end
def expect_no_internal_tracking
expect(Gitlab::InternalEvents).not_to receive(:track_event)
end
let(:all_time_total_count) { Gitlab::Usage::EventSelectionRule.new(name: event_name, time_framed: false) }
let(:time_framed_total_count) { Gitlab::Usage::EventSelectionRule.new(name: event_name, time_framed: true) }
before do
allow(Gitlab::Tracking::EventDefinition).to receive(:internal_event_exists?).with('an_event').and_return(true)
event_definition = instance_double(
Gitlab::Tracking::EventDefinition,
event_selection_rules: [all_time_total_count, time_framed_total_count],
to_h: {}
)
allow(Gitlab::Tracking::EventDefinition).to receive(:find).with(event_name).and_return(event_definition)
end
context 'when user is logged in' do
let(:namespace) { project.namespace }
before do
sign_in(user)
end
it 'tracks the events if DNT is disabled' do
stub_do_not_track('0')
expect_internal_tracking
subject
end
it 'does not track the event if DNT is enabled' do
stub_do_not_track('1')
expect_no_internal_tracking
subject
end
it 'does not track the event if the format is not HTML' do
expect_no_internal_tracking
get :show, params: { id: 1, format: :json }
end
it 'does not track the event if a custom condition returns false' do
allow(controller).to receive(:custom_condition?).and_return(false)
expect_no_internal_tracking
subject
end
end
context 'when user is not logged in' do
let(:user) { nil }
it 'tracks internal event' do
expect_internal_tracking
get :index
end
it 'tracks total Redis counters' do
expect(all_time_total_count).to receive(:redis_key_for_date).and_call_original
expect(time_framed_total_count).to receive(:redis_key_for_date).and_call_original
get :index
end
it 'does not update unique counter' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
get :index
end
end
end
describe '.track_event' do
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: 'an_event',
action: 'an_action',
label: 'a_label',
destinations: [:redis_hll, :snowplow],
conditions: [:custom_condition_one?, :custom_condition_two?]
) { |controller| controller.get_custom_id }
track_internal_event :internal_event, name: 'an_internal_event'
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 tracking_project_source
Project.first
end
def custom_condition_one?
true
end
def custom_condition_two?
true
end
end
def expect_redis_hll_tracking
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to have_received(:track_event)
.with(event_name, values: instance_of(String))
end
def expect_snowplow_tracking(user)
context = Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event_name)
.to_context.to_json
expect_snowplow_event(
category: anything,
action: event_action,
property: event_name,
label: event_label,
project: project,
namespace: group,
user: user,
context: [context]
)
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_redis_hll_tracking
expect_snowplow_tracking(user)
end
it 'tracks the event if DNT is not enabled' do
stub_do_not_track('0')
get :index
expect_redis_hll_tracking
expect_snowplow_tracking(user)
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_redis_hll_tracking
expect_snowplow_tracking(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_redis_hll_tracking
expect_snowplow_tracking(nil)
end
context 'when there is no custom_id set' do
before do
allow(controller).to receive(:get_custom_id).and_return(nil)
get :show, params: { id: 2 }
end
it 'does not track the HLL event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
end
it 'tracks Snowplow event' do
expect_snowplow_tracking(nil)
end
end
end
end
end
|