Commit c28c03e0 authored by Stan Hu's avatar Stan Hu

Merge branch 'enhance-redis-tracker' into 'master'

Add a block argument in track_redis_hll_event

See merge request gitlab-org/gitlab!56250
parents 3fe6077f bfb9e88a
...@@ -10,26 +10,31 @@ ...@@ -10,26 +10,31 @@
# track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score' # track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score'
# #
# You can also pass custom conditions using `if:`, using the same format as with Rails callbacks. # You can also pass custom conditions using `if:`, using the same format as with Rails callbacks.
# You can also pass an optional block that calculates and returns a custom id to track.
module RedisTracking module RedisTracking
extend ActiveSupport::Concern extend ActiveSupport::Concern
class_methods do class_methods do
def track_redis_hll_event(*controller_actions, name:, if: nil) def track_redis_hll_event(*controller_actions, name:, if: nil, &block)
custom_conditions = Array.wrap(binding.local_variable_get('if')) custom_conditions = Array.wrap(binding.local_variable_get('if'))
conditions = [:trackable_request?, *custom_conditions] conditions = [:trackable_request?, *custom_conditions]
after_action only: controller_actions, if: conditions do after_action only: controller_actions, if: conditions do
track_unique_redis_hll_event(name) track_unique_redis_hll_event(name, &block)
end end
end end
end end
private private
def track_unique_redis_hll_event(event_name) def track_unique_redis_hll_event(event_name, &block)
return unless visitor_id custom_id = block_given? ? yield(self) : nil
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: visitor_id) unique_id = custom_id || visitor_id
return unless unique_id
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: unique_id)
end end
def trackable_request? def trackable_request?
......
...@@ -502,13 +502,14 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF ...@@ -502,13 +502,14 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
Use one of the following methods to track events: Use one of the following methods to track events:
1. Track event in controller using `RedisTracking` module with `track_redis_hll_event(*controller_actions, name:, if: nil)`. 1. Track event in controller using `RedisTracking` module with `track_redis_hll_event(*controller_actions, name:, if: nil, &block)`.
Arguments: Arguments:
- `controller_actions`: controller actions we want to track. - `controller_actions`: controller actions we want to track.
- `name`: event name. - `name`: event name.
- `if`: optional custom conditions, using the same format as with Rails callbacks. - `if`: optional custom conditions, using the same format as with Rails callbacks.
- `&block`: optional block that computes and returns the `custom_id` that we want to track. This will override the `visitor_id`.
Example usage: Example usage:
......
...@@ -9,8 +9,8 @@ RSpec.describe RedisTracking do ...@@ -9,8 +9,8 @@ RSpec.describe RedisTracking do
include RedisTracking include RedisTracking
skip_before_action :authenticate_user!, only: :show skip_before_action :authenticate_user!, only: :show
track_redis_hll_event :index, :show, name: 'g_compliance_approval_rules', track_redis_hll_event(:index, :show, name: 'g_compliance_approval_rules',
if: [:custom_condition_one?, :custom_condition_two?] if: [:custom_condition_one?, :custom_condition_two?]) { |controller| controller.get_custom_id }
def index def index
render html: 'index' render html: 'index'
...@@ -24,6 +24,10 @@ RSpec.describe RedisTracking do ...@@ -24,6 +24,10 @@ RSpec.describe RedisTracking do
render html: 'show' render html: 'show'
end end
def get_custom_id
'some_custom_id'
end
private private
def custom_condition_one? def custom_condition_one?
...@@ -92,19 +96,15 @@ RSpec.describe RedisTracking do ...@@ -92,19 +96,15 @@ RSpec.describe RedisTracking do
end end
end end
context 'when user is not logged in and there is a visitor_id' do context 'when user is not logged in' do
let(:visitor_id) { SecureRandom.uuid } let(:visitor_id) { SecureRandom.uuid }
before do it 'tracks the event when there is a visitor id' do
routes.draw { get 'show' => 'anonymous#show' }
end
it 'tracks the event' do
cookies[:visitor_id] = { value: visitor_id, expires: 24.months } cookies[:visitor_id] = { value: visitor_id, expires: 24.months }
expect_tracking expect_tracking
get :show get :show, params: { id: 1 }
end end
end end
...@@ -114,5 +114,19 @@ RSpec.describe RedisTracking do ...@@ -114,5 +114,19 @@ RSpec.describe RedisTracking do
get :index get :index
end end
it 'tracks the event when there is custom id' do
expect_tracking
get :show, params: { id: 1 }
end
it 'does not track the event when there is no custom id' do
expect(controller).to receive(:get_custom_id).and_return(nil)
expect_no_tracking
get :show, params: { id: 2 }
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment