From a09983ae35713f5a2bbb100981116d31ce99826e Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 20 Jul 2020 12:26:25 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-2-stable-ee --- app/helpers/analytics/unique_visits_helper.rb | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/helpers/analytics/unique_visits_helper.rb (limited to 'app/helpers/analytics') diff --git a/app/helpers/analytics/unique_visits_helper.rb b/app/helpers/analytics/unique_visits_helper.rb new file mode 100644 index 00000000000..ded7f54e44e --- /dev/null +++ b/app/helpers/analytics/unique_visits_helper.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module Analytics + module UniqueVisitsHelper + extend ActiveSupport::Concern + + 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 + + def track_visit(target_id) + return unless Feature.enabled?(:track_unique_visits) + return unless Gitlab::CurrentSettings.usage_ping_enabled? + return unless visitor_id + + Gitlab::Analytics::UniqueVisits.new.track_visit(visitor_id, target_id) + end + + 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 + track_visit(target_id) + end + end + end + end +end -- cgit v1.2.3