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 @@
# 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 an optional block that calculates and returns a custom id to track.
module RedisTracking
extend ActiveSupport::Concern
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'))
conditions = [:trackable_request?, *custom_conditions]
after_action only: controller_actions, if: conditions do
track_unique_redis_hll_event(name)
track_unique_redis_hll_event(name, &block)
end
end
end
private
def track_unique_redis_hll_event(event_name)
return unless visitor_id
def track_unique_redis_hll_event(event_name, &block)
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
def trackable_request?
......
......@@ -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:
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:
- `controller_actions`: controller actions we want to track.
- `name`: event name.
- `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:
......
......@@ -9,8 +9,8 @@ RSpec.describe RedisTracking do
include RedisTracking
skip_before_action :authenticate_user!, only: :show
track_redis_hll_event :index, :show, name: 'g_compliance_approval_rules',
if: [:custom_condition_one?, :custom_condition_two?]
track_redis_hll_event(:index, :show, name: 'g_compliance_approval_rules',
if: [:custom_condition_one?, :custom_condition_two?]) { |controller| controller.get_custom_id }
def index
render html: 'index'
......@@ -24,6 +24,10 @@ RSpec.describe RedisTracking do
render html: 'show'
end
def get_custom_id
'some_custom_id'
end
private
def custom_condition_one?
......@@ -92,19 +96,15 @@ RSpec.describe RedisTracking do
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 }
before do
routes.draw { get 'show' => 'anonymous#show' }
end
it 'tracks the event' do
it 'tracks the event when there is a visitor id' do
cookies[:visitor_id] = { value: visitor_id, expires: 24.months }
expect_tracking
get :show
get :show, params: { id: 1 }
end
end
......@@ -114,5 +114,19 @@ RSpec.describe RedisTracking do
get :index
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
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