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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.7 KiB
Ruby
Raw Normal View History

2022-05-07 20:08:51 +05:30
# frozen_string_literal: true
module ProductAnalyticsTracking
include Gitlab::Tracking::Helpers
extend ActiveSupport::Concern
class_methods do
2023-05-27 22:25:52 +05:30
def track_event(*controller_actions, name:, action: nil, label: nil, conditions: nil, destinations: [:redis_hll], &block)
2022-05-07 20:08:51 +05:30
custom_conditions = [:trackable_html_request?, *conditions]
after_action only: controller_actions, if: custom_conditions do
2023-05-27 22:25:52 +05:30
route_events_to(destinations, name, action, label, &block)
2022-08-27 11:52:29 +05:30
end
end
2022-05-07 20:08:51 +05:30
end
private
2023-05-27 22:25:52 +05:30
def route_events_to(destinations, name, action, label, &block)
2022-08-27 11:52:29 +05:30
track_unique_redis_hll_event(name, &block) if destinations.include?(:redis_hll)
2023-06-20 00:43:36 +05:30
return unless destinations.include?(:snowplow)
2023-05-27 22:25:52 +05:30
raise "action is required when destination is snowplow" unless action
raise "label is required when destination is snowplow" unless label
2022-08-27 11:52:29 +05:30
optional_arguments = {
namespace: tracking_namespace_source,
project: tracking_project_source
}.compact
Gitlab::Tracking.event(
self.class.to_s,
action,
user: current_user,
property: name,
label: label,
2022-11-25 23:54:43 +05:30
context: [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: name).to_context],
2022-08-27 11:52:29 +05:30
**optional_arguments
)
end
2023-07-09 08:55:56 +05:30
def track_unique_redis_hll_event(event_name, &block)
custom_id = block ? yield(self) : nil
unique_id = custom_id || visitor_id
return unless unique_id
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: unique_id)
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
2022-05-07 20:08:51 +05:30
end