Commit 2921bcb7 authored by Sean McGivern's avatar Sean McGivern

Merge branch '295265-reuse-dnt-enabled' into 'master'

Add reusable Gitlab::Tracking::Helpers module

See merge request gitlab-org/gitlab!64169
parents 57a32c54 fb3ad067
......@@ -21,8 +21,6 @@ class Admin::CohortsController < Admin::ApplicationController
end
def track_cohorts_visit
if request.format.html? && request.headers['DNT'] != '1'
track_visit('i_analytics_cohorts')
end
track_visit('i_analytics_cohorts') if trackable_html_request?
end
end
......@@ -12,12 +12,13 @@
# 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
include Gitlab::Tracking::Helpers
extend ActiveSupport::Concern
class_methods do
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]
conditions = [:trackable_html_request?, *custom_conditions]
after_action only: controller_actions, if: conditions do
track_unique_redis_hll_event(name, &block)
......@@ -37,10 +38,6 @@ module RedisTracking
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: unique_id)
end
def trackable_request?
request.format.html? && request.headers['DNT'] != '1'
end
def visitor_id
return cookies[:visitor_id] if cookies[:visitor_id].present?
return unless current_user
......
......@@ -193,7 +193,7 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
end
def track_viewed_diffs_events
return if request.headers['DNT'] == '1'
return if dnt_enabled?
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
.track_mr_diffs_action(merge_request: @merge_request)
......
......@@ -2,6 +2,7 @@
module Analytics
module UniqueVisitsHelper
include Gitlab::Tracking::Helpers
extend ActiveSupport::Concern
def visitor_id
......@@ -21,7 +22,7 @@ module Analytics
class_methods do
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)
end
end
......
......@@ -11,6 +11,7 @@ module Gitlab
module Experimentation
module ControllerConcern
include ::Gitlab::Experimentation::GroupTypes
include Gitlab::Tracking::Helpers
extend ActiveSupport::Concern
included do
......@@ -101,10 +102,6 @@ module Gitlab
private
def dnt_enabled?
Gitlab::Utils.to_boolean(request.headers['DNT'])
end
def experimentation_subject_id
cookies.signed[:experimentation_subject_id]
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