From f2afa7450d6fd68ce221be2fb530a5bdd2bf206d Mon Sep 17 00:00:00 2001
From: Ankit Panchal <apanchal@gitlab.com>
Date: Thu, 5 Oct 2023 18:58:16 +0000
Subject: [PATCH] Adding new feature flag gl_analytics_tracking

---
 app/views/layouts/_snowplow.html.haml                 |  2 +-
 .../development/gl_analytics_tracking.yml             |  8 ++++++++
 lib/gitlab/content_security_policy/config_loader.rb   |  2 +-
 lib/gitlab/gon_helper.rb                              |  4 +++-
 .../content_security_policy/config_loader_spec.rb     | 11 -----------
 spec/lib/gitlab/gon_helper_spec.rb                    |  1 +
 6 files changed, 14 insertions(+), 14 deletions(-)
 create mode 100644 config/feature_flags/development/gl_analytics_tracking.yml

diff --git a/app/views/layouts/_snowplow.html.haml b/app/views/layouts/_snowplow.html.haml
index 10b2002dfef0e..3582deea902e2 100644
--- a/app/views/layouts/_snowplow.html.haml
+++ b/app/views/layouts/_snowplow.html.haml
@@ -2,7 +2,7 @@
 
 - namespace = @group || @project&.namespace || @namespace
 = webpack_bundle_tag 'tracker'
-- if Gitlab.com? && Feature.enabled?(:browsersdk_tracking)
+- if Gitlab.com? && Feature.enabled?(:browsersdk_tracking) && Feature.enabled?(:gl_analytics_tracking, Feature.current_request)
   = webpack_bundle_tag 'analytics'
 = javascript_tag do
   :plain
diff --git a/config/feature_flags/development/gl_analytics_tracking.yml b/config/feature_flags/development/gl_analytics_tracking.yml
new file mode 100644
index 0000000000000..3a67aa058129e
--- /dev/null
+++ b/config/feature_flags/development/gl_analytics_tracking.yml
@@ -0,0 +1,8 @@
+---
+name: gl_analytics_tracking
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/132534
+rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/426106
+milestone: '16.5'
+type: development
+group: group::analytics instrumentation
+default_enabled: false
diff --git a/lib/gitlab/content_security_policy/config_loader.rb b/lib/gitlab/content_security_policy/config_loader.rb
index 87a6d4ada706c..f7c9d95c53c6b 100644
--- a/lib/gitlab/content_security_policy/config_loader.rb
+++ b/lib/gitlab/content_security_policy/config_loader.rb
@@ -86,7 +86,7 @@ def allow_snowplow_micro(directives)
 
         def add_browsersdk_tracking(directives)
           return if directives.blank?
-          return unless Gitlab.com? && Feature.enabled?(:browsersdk_tracking) && ENV['GITLAB_ANALYTICS_URL'].present?
+          return unless Gitlab.com? && ENV['GITLAB_ANALYTICS_URL'].present?
 
           default_connect_src = directives['connect-src'] || directives['default-src']
           connect_src_values = Array.wrap(default_connect_src) | [ENV['GITLAB_ANALYTICS_URL']]
diff --git a/lib/gitlab/gon_helper.rb b/lib/gitlab/gon_helper.rb
index 47fb39f15acf8..a7f8b93ee102d 100644
--- a/lib/gitlab/gon_helper.rb
+++ b/lib/gitlab/gon_helper.rb
@@ -120,7 +120,9 @@ def default_avatar_url
     end
 
     def add_browsersdk_tracking
-      return unless Gitlab.com? && Feature.enabled?(:browsersdk_tracking)
+      return unless Gitlab.com? && Feature.enabled?(:browsersdk_tracking) && Feature.enabled?(:gl_analytics_tracking,
+Feature.current_request)
+
       return if ENV['GITLAB_ANALYTICS_URL'].blank? || ENV['GITLAB_ANALYTICS_ID'].blank?
 
       gon.analytics_url = ENV['GITLAB_ANALYTICS_URL']
diff --git a/spec/lib/gitlab/content_security_policy/config_loader_spec.rb b/spec/lib/gitlab/content_security_policy/config_loader_spec.rb
index 3682a65418164..9e2f3bda14cf1 100644
--- a/spec/lib/gitlab/content_security_policy/config_loader_spec.rb
+++ b/spec/lib/gitlab/content_security_policy/config_loader_spec.rb
@@ -577,17 +577,6 @@
         end
       end
 
-      context 'when browsersdk_tracking is disabled' do
-        before do
-          stub_feature_flags(browsersdk_tracking: false)
-          stub_env('GITLAB_ANALYTICS_URL', analytics_url)
-        end
-
-        it 'does not add GITLAB_ANALYTICS_URL to connect-src' do
-          expect(connect_src).not_to include(analytics_url)
-        end
-      end
-
       context 'when GITLAB_ANALYTICS_URL is not set' do
         before do
           stub_env('GITLAB_ANALYTICS_URL', nil)
diff --git a/spec/lib/gitlab/gon_helper_spec.rb b/spec/lib/gitlab/gon_helper_spec.rb
index fc722402917b7..e4684597ddf69 100644
--- a/spec/lib/gitlab/gon_helper_spec.rb
+++ b/spec/lib/gitlab/gon_helper_spec.rb
@@ -206,6 +206,7 @@
       context 'when feature flag is false' do
         before do
           stub_feature_flags(browsersdk_tracking: false)
+          stub_feature_flags(gl_analytics_tracking: false)
         end
 
         it "doesn't set the analytics_url and analytics_id" do
-- 
GitLab