From 0a3b623f1ce9778f47cd7db50580fafbbb6076b7 Mon Sep 17 00:00:00 2001
From: Miranda Fluharty <mfluharty@gitlab.com>
Date: Tue, 11 Mar 2025 07:15:39 -0600
Subject: [PATCH] Track security inventory page views

Use `scripts/internal_events/cli.rb` to generate an event and a metric
Track the metric on #show in the controller
Add test to see that the metric was tracked
---
 config/metrics/schema/product_groups.json     |  1 +
 .../groups/security/inventory_controller.rb   | 10 ++++++++
 .../events/view_group_security_inventory.yml  | 14 +++++++++++
 ..._id_from_view_group_security_inventory.yml | 21 ++++++++++++++++
 .../security/inventory_controller_spec.rb     | 24 +++++++++++++++++++
 5 files changed, 70 insertions(+)
 create mode 100644 ee/config/events/view_group_security_inventory.yml
 create mode 100644 ee/config/metrics/counts_all/count_distinct_user_id_from_view_group_security_inventory.yml
 create mode 100644 ee/spec/requests/groups/security/inventory_controller_spec.rb

diff --git a/config/metrics/schema/product_groups.json b/config/metrics/schema/product_groups.json
index bed096ed42511..a17f6a692acb1 100644
--- a/config/metrics/schema/product_groups.json
+++ b/config/metrics/schema/product_groups.json
@@ -61,6 +61,7 @@
     "secret_detection",
     "security_infrastructure",
     "security_insights",
+    "security_platform_management",
     "security_policies",
     "self_managed",
     "source_code",
diff --git a/ee/app/controllers/groups/security/inventory_controller.rb b/ee/app/controllers/groups/security/inventory_controller.rb
index 98e3223fe8a2c..5297c2c49e14a 100644
--- a/ee/app/controllers/groups/security/inventory_controller.rb
+++ b/ee/app/controllers/groups/security/inventory_controller.rb
@@ -13,6 +13,10 @@ class InventoryController < Groups::ApplicationController
 
       feature_category :security_asset_inventories
 
+      include ProductAnalyticsTracking
+
+      track_internal_event :show, name: 'view_group_security_inventory'
+
       def show; end
 
       private
@@ -21,6 +25,12 @@ def ensure_feature_available!
         render_404 unless License.feature_available?(:security_inventory) &&
           ::Feature.enabled?(:security_inventory_dashboard, group, type: :wip)
       end
+
+      def tracking_namespace_source
+        group
+      end
+
+      def tracking_project_source; end
     end
   end
 end
diff --git a/ee/config/events/view_group_security_inventory.yml b/ee/config/events/view_group_security_inventory.yml
new file mode 100644
index 0000000000000..c2e105ac4a2cf
--- /dev/null
+++ b/ee/config/events/view_group_security_inventory.yml
@@ -0,0 +1,14 @@
+---
+description: Group Security Inventory page was viewed
+internal_events: true
+action: view_group_security_inventory
+identifiers:
+- namespace
+- user
+product_group: security_platform_management
+product_categories:
+- security_asset_inventories
+milestone: '17.10'
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/183563
+tiers:
+- ultimate
diff --git a/ee/config/metrics/counts_all/count_distinct_user_id_from_view_group_security_inventory.yml b/ee/config/metrics/counts_all/count_distinct_user_id_from_view_group_security_inventory.yml
new file mode 100644
index 0000000000000..9cbfc8801f35f
--- /dev/null
+++ b/ee/config/metrics/counts_all/count_distinct_user_id_from_view_group_security_inventory.yml
@@ -0,0 +1,21 @@
+---
+key_path: redis_hll_counters.count_distinct_user_id_from_view_group_security_inventory
+description: Count of unique users who viewed the group security inventory page
+product_group: security_platform_management
+product_categories:
+- security_asset_inventories
+performance_indicator_type: []
+value_type: number
+status: active
+milestone: '17.10'
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/183563
+time_frame:
+- 28d
+- 7d
+data_source: internal_events
+data_category: optional
+tiers:
+- ultimate
+events:
+- name: view_group_security_inventory
+  unique: user.id
diff --git a/ee/spec/requests/groups/security/inventory_controller_spec.rb b/ee/spec/requests/groups/security/inventory_controller_spec.rb
new file mode 100644
index 0000000000000..95ec35977bc5e
--- /dev/null
+++ b/ee/spec/requests/groups/security/inventory_controller_spec.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Groups::Security::InventoryController, feature_category: :security_asset_inventories do
+  let_it_be(:user) { create(:user) }
+  let_it_be(:group) { create(:group, owners: user) }
+
+  before do
+    stub_licensed_features(security_inventory: true)
+    stub_feature_flags(security_inventory_dashboard: true)
+
+    sign_in(user)
+  end
+
+  describe '#show', :aggregate_failures do
+    it_behaves_like 'internal event tracking' do
+      let(:event) { 'view_group_security_inventory' }
+      let(:namespace) { group }
+
+      subject(:request) { get group_security_inventory_path(group) }
+    end
+  end
+end
-- 
GitLab