Commit fb3ad067 authored by Patrick Bajao's avatar Patrick Bajao

Add reuasable Gitlab::Tracking::Helpers module

There are several controllers/concerns/modules that checks for the
DNT header and the logic are the same.

To make them DRY, a new Gitlab::Tracking::Helpers module was added
to encapsulate the following methods:
- `dnt_enabled?`
- `trackable_html_request?`
parent 7b051af7
...@@ -21,8 +21,6 @@ class Admin::CohortsController < Admin::ApplicationController ...@@ -21,8 +21,6 @@ class Admin::CohortsController < Admin::ApplicationController
end end
def track_cohorts_visit def track_cohorts_visit
if request.format.html? && request.headers['DNT'] != '1' track_visit('i_analytics_cohorts') if trackable_html_request?
track_visit('i_analytics_cohorts')
end
end end
end end
...@@ -12,12 +12,13 @@ ...@@ -12,12 +12,13 @@
# 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. # You can also pass an optional block that calculates and returns a custom id to track.
module RedisTracking module RedisTracking
include Gitlab::Tracking::Helpers
extend ActiveSupport::Concern extend ActiveSupport::Concern
class_methods do class_methods do
def track_redis_hll_event(*controller_actions, name:, if: nil, &block) 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_html_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, &block) track_unique_redis_hll_event(name, &block)
...@@ -37,10 +38,6 @@ module RedisTracking ...@@ -37,10 +38,6 @@ module RedisTracking
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: unique_id) Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: unique_id)
end end
def trackable_request?
request.format.html? && request.headers['DNT'] != '1'
end
def visitor_id def visitor_id
return cookies[:visitor_id] if cookies[:visitor_id].present? return cookies[:visitor_id] if cookies[:visitor_id].present?
return unless current_user return unless current_user
......
...@@ -193,7 +193,7 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic ...@@ -193,7 +193,7 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
end end
def track_viewed_diffs_events def track_viewed_diffs_events
return if request.headers['DNT'] == '1' return if dnt_enabled?
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
.track_mr_diffs_action(merge_request: @merge_request) .track_mr_diffs_action(merge_request: @merge_request)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
module Analytics module Analytics
module UniqueVisitsHelper module UniqueVisitsHelper
include Gitlab::Tracking::Helpers
extend ActiveSupport::Concern extend ActiveSupport::Concern
def visitor_id def visitor_id
...@@ -21,7 +22,7 @@ module Analytics ...@@ -21,7 +22,7 @@ module Analytics
class_methods do class_methods do
def track_unique_visits(controller_actions, target_id:) def track_unique_visits(controller_actions, target_id:)
after_action only: controller_actions, if: -> { request.format.html? && request.headers['DNT'] != '1' } do after_action only: controller_actions, if: -> { trackable_html_request? } do
track_visit(target_id) track_visit(target_id)
end end
end end
......
...@@ -11,6 +11,7 @@ module Gitlab ...@@ -11,6 +11,7 @@ module Gitlab
module Experimentation module Experimentation
module ControllerConcern module ControllerConcern
include ::Gitlab::Experimentation::GroupTypes include ::Gitlab::Experimentation::GroupTypes
include Gitlab::Tracking::Helpers
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
...@@ -101,10 +102,6 @@ module Gitlab ...@@ -101,10 +102,6 @@ module Gitlab
private private
def dnt_enabled?
Gitlab::Utils.to_boolean(request.headers['DNT'])
end
def experimentation_subject_id def experimentation_subject_id
cookies.signed[:experimentation_subject_id] cookies.signed[:experimentation_subject_id]
end end
......
# frozen_string_literal: true
module Gitlab
module Tracking
module Helpers
def dnt_enabled?
Gitlab::Utils.to_boolean(request.headers['DNT'])
end
def trackable_html_request?
request.format.html? && !dnt_enabled?
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