Commit b18acc2d authored by Alper Akgun's avatar Alper Akgun

Merge branch '498-migrate-first-metric-from-rhll-to-snowplow' into 'master'

Migrate  g_analytics_valuestream metric to Snowplow

See merge request gitlab-org/gitlab!82264
parents 379a75a3 56c29ef1
# frozen_string_literal: true
module ProductAnalyticsTracking
include Gitlab::Tracking::Helpers
include RedisTracking
extend ActiveSupport::Concern
class_methods do
def track_event(*controller_actions, name:, conditions: nil, destinations: [:redis_hll], &block)
custom_conditions = [:trackable_html_request?, *conditions]
after_action only: controller_actions, if: custom_conditions do
route_events_to(destinations, name, &block)
end
end
end
private
def route_events_to(destinations, name, &block)
track_unique_redis_hll_event(name, &block) if destinations.include?(:redis_hll)
if destinations.include?(:snowplow) && Feature.enabled?(:route_hll_to_snowplow, tracking_namespace_source, default_enabled: :yaml)
Gitlab::Tracking.event(self.class.to_s, name, namespace: tracking_namespace_source, user: current_user)
end
end
end
---
name: route_hll_to_snowplow
introduced_by_url: https://gitlab.com/gitlab-org/product-intelligence/-/issues/498
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/354442
milestone: '14.9'
type: development
group: group::product intelligence
default_enabled: false
...@@ -23,7 +23,7 @@ class Groups::Analytics::ApplicationController < ApplicationController ...@@ -23,7 +23,7 @@ class Groups::Analytics::ApplicationController < ApplicationController
def load_group def load_group
return unless params['group_id'] return unless params['group_id']
@group = find_routable!(Group, params['group_id'], request.fullpath) @group ||= find_routable!(Group, params['group_id'], request.fullpath)
end end
def load_project def load_project
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
class Groups::Analytics::CycleAnalyticsController < Groups::Analytics::ApplicationController class Groups::Analytics::CycleAnalyticsController < Groups::Analytics::ApplicationController
include CycleAnalyticsParams include CycleAnalyticsParams
include RedisTracking include ProductAnalyticsTracking
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
increment_usage_counter Gitlab::UsageDataCounters::CycleAnalyticsCounter, :views, only: :show increment_usage_counter Gitlab::UsageDataCounters::CycleAnalyticsCounter, :views, only: :show
...@@ -22,7 +22,7 @@ class Groups::Analytics::CycleAnalyticsController < Groups::Analytics::Applicati ...@@ -22,7 +22,7 @@ class Groups::Analytics::CycleAnalyticsController < Groups::Analytics::Applicati
layout 'group' layout 'group'
track_redis_hll_event :show, name: 'g_analytics_valuestream' track_event :show, name: 'g_analytics_valuestream', destinations: [:redis_hll, :snowplow]
def show def show
epic_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer">'.html_safe % { url: "https://gitlab.com/groups/gitlab-org/-/epics/6046" } epic_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer">'.html_safe % { url: "https://gitlab.com/groups/gitlab-org/-/epics/6046" }
...@@ -55,4 +55,6 @@ class Groups::Analytics::CycleAnalyticsController < Groups::Analytics::Applicati ...@@ -55,4 +55,6 @@ class Groups::Analytics::CycleAnalyticsController < Groups::Analytics::Applicati
@group.value_streams.find(params[:value_stream_id]) @group.value_streams.find(params[:value_stream_id])
end end
end end
alias_method :tracking_namespace_source, :load_group
end end
...@@ -57,6 +57,25 @@ RSpec.describe Groups::Analytics::CycleAnalyticsController do ...@@ -57,6 +57,25 @@ RSpec.describe Groups::Analytics::CycleAnalyticsController do
end end
end end
end end
describe 'tracking events', :snowplow do
it 'tracks redis hll event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with('g_analytics_valuestream', { values: anything })
get(:show, params: { group_id: group })
end
it 'tracks snowplow event' do
get(:show, params: { group_id: group })
expect_snowplow_event(
category: 'Groups::Analytics::CycleAnalyticsController',
action: 'g_analytics_valuestream',
namespace: group,
user: user
)
end
end
end end
context 'when the license is missing' do context 'when the license is missing' do
......
# frozen_string_literal: true
require "spec_helper"
RSpec.describe ProductAnalyticsTracking, :snowplow do
include TrackingHelpers
include SnowplowHelpers
let(:user) { create(:user) }
let!(:group) { create(:group) }
before do
allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
end
controller(ApplicationController) do
include ProductAnalyticsTracking
skip_before_action :authenticate_user!, only: :show
track_event(:index, :show, name: 'g_analytics_valuestream', destinations: [:redis_hll, :snowplow],
conditions: [:custom_condition_one?, :custom_condition_two?]) { |controller| controller.get_custom_id }
def index
render html: 'index'
end
def new
render html: 'new'
end
def show
render html: 'show'
end
def get_custom_id
'some_custom_id'
end
private
def tracking_namespace_source
Group.first
end
def custom_condition_one?
true
end
def custom_condition_two?
true
end
end
def expect_tracking(user: self.user)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to have_received(:track_event)
.with('g_analytics_valuestream', values: instance_of(String))
expect_snowplow_event(
category: anything,
action: 'g_analytics_valuestream',
namespace: group,
user: user
)
end
def expect_no_tracking
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
expect_no_snowplow_event
end
context 'when user is logged in' do
before do
sign_in(user)
end
it 'tracks the event' do
get :index
expect_tracking
end
context 'when FF is disabled' do
before do
stub_feature_flags(route_hll_to_snowplow: false)
end
it 'doesnt track snowplow event' do
get :index
expect_no_snowplow_event
end
end
it 'tracks the event if DNT is not enabled' do
stub_do_not_track('0')
get :index
expect_tracking
end
it 'does not track the event if DNT is enabled' do
stub_do_not_track('1')
get :index
expect_no_tracking
end
it 'does not track the event if the format is not HTML' do
get :index, format: :json
expect_no_tracking
end
it 'does not track the event if a custom condition returns false' do
allow(controller).to receive(:custom_condition_two?).and_return(false)
get :index
expect_no_tracking
end
it 'does not track the event for untracked actions' do
get :new
expect_no_tracking
end
end
context 'when user is not logged in' do
let(:visitor_id) { SecureRandom.uuid }
it 'tracks the event when there is a visitor id' do
cookies[:visitor_id] = { value: visitor_id, expires: 24.months }
get :show, params: { id: 1 }
expect_tracking(user: nil)
end
end
context 'when user is not logged in and there is no visitor_id' do
it 'does not track the event' do
get :index
expect_no_tracking
end
it 'tracks the event when there is custom id' do
get :show, params: { id: 1 }
expect_tracking(user: nil)
end
it 'does not track the HLL event when there is no custom id' do
allow(controller).to receive(:get_custom_id).and_return(nil)
get :show, params: { id: 2 }
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
expect_snowplow_event(
category: anything,
action: 'g_analytics_valuestream',
namespace: group,
user: nil
)
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