debian-mirror-gitlab/app/controllers/concerns/redis_tracking.rb

50 lines
1.3 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
# Example:
#
# # In controller include module
# # Track event for index action
#
# include RedisTracking
#
2021-03-11 19:13:27 +05:30
# track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score'
2021-01-03 14:25:43 +05:30
#
# You can also pass custom conditions using `if:`, using the same format as with Rails callbacks.
2021-04-29 21:17:54 +05:30
# You can also pass an optional block that calculates and returns a custom id to track.
2020-11-24 15:15:51 +05:30
module RedisTracking
2021-09-30 23:02:18 +05:30
include Gitlab::Tracking::Helpers
2020-11-24 15:15:51 +05:30
extend ActiveSupport::Concern
class_methods do
2021-04-29 21:17:54 +05:30
def track_redis_hll_event(*controller_actions, name:, if: nil, &block)
2021-01-03 14:25:43 +05:30
custom_conditions = Array.wrap(binding.local_variable_get('if'))
2021-09-30 23:02:18 +05:30
conditions = [:trackable_html_request?, *custom_conditions]
2021-01-03 14:25:43 +05:30
after_action only: controller_actions, if: conditions do
2021-04-29 21:17:54 +05:30
track_unique_redis_hll_event(name, &block)
2020-11-24 15:15:51 +05:30
end
end
end
private
2021-04-29 21:17:54 +05:30
def track_unique_redis_hll_event(event_name, &block)
2022-08-27 11:52:29 +05:30
custom_id = block ? yield(self) : nil
2020-11-24 15:15:51 +05:30
2021-04-29 21:17:54 +05:30
unique_id = custom_id || visitor_id
return unless unique_id
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: unique_id)
2020-11-24 15:15:51 +05:30
end
def visitor_id
return cookies[:visitor_id] if cookies[:visitor_id].present?
return unless current_user
uuid = SecureRandom.uuid
cookies[:visitor_id] = { value: uuid, expires: 24.months }
uuid
end
end