diff --git a/ee/app/models/ee/project.rb b/ee/app/models/ee/project.rb index bac5237926cd7ce635f49665d6746ae30420f9b7..0f139bf6fff15269a21a2226ad51395dfeff9114 100644 --- a/ee/app/models/ee/project.rb +++ b/ee/app/models/ee/project.rb @@ -1205,7 +1205,7 @@ def security_policy_bot end def product_analytics_events_used(year: Time.current.year, month: Time.current.month) - return 0 unless ::ProductAnalytics::Settings.new(project: self).enabled? + return unless ::ProductAnalytics::Settings.new(project: self).enabled? && self.project_setting&.product_analytics_instrumentation_key.present? ::Analytics::ProductAnalytics::ProjectUsageData.new(project_id: id) .events_stored_count(year: year, month: month) diff --git a/ee/spec/models/ee/project_spec.rb b/ee/spec/models/ee/project_spec.rb index 4236641e79067a1a913ff8ec68d4d064204e57b9..7239c937dff013e23fea1ff35d41582daf02aec6 100644 --- a/ee/spec/models/ee/project_spec.rb +++ b/ee/spec/models/ee/project_spec.rb @@ -4236,31 +4236,45 @@ def stub_default_url_options(host) end end - context 'when month and year is overridden' do - subject { project.product_analytics_events_used(year: 2025, month: 10) } + context 'when project is onboarded with product analytics' do + before do + project.project_setting.update!(product_analytics_instrumentation_key: 'abc-123') + end - it 'queries the ProjectUsageData for the project' do - expect_next_instance_of(Analytics::ProductAnalytics::ProjectUsageData) do |instance| - expect(instance).to receive(:events_stored_count).with(month: 10, year: 2025).once - end + context 'when month and year is overridden' do + subject { project.product_analytics_events_used(year: 2025, month: 10) } - subject + it 'queries the ProjectUsageData for the project' do + expect_next_instance_of(Analytics::ProductAnalytics::ProjectUsageData) do |instance| + expect(instance).to receive(:events_stored_count).with(month: 10, year: 2025).once + end + + subject + end end - end - context 'when using default time period' do - it 'queries the ProjectUsageData for the project' do - expect_next_instance_of(Analytics::ProductAnalytics::ProjectUsageData) do |instance| - expect(instance).to receive(:events_stored_count).once + context 'when using default time period' do + it 'queries the ProjectUsageData for the project' do + expect_next_instance_of(Analytics::ProductAnalytics::ProjectUsageData) do |instance| + expect(instance).to receive(:events_stored_count).once + end + + subject end + end + end - subject + context 'when project is not onboarded with product analytics' do + before do + project.project_setting.update!(product_analytics_instrumentation_key: nil) end + + it { is_expected.to be_nil } end end context 'when product analytics is not enabled' do - it { is_expected.to be_zero } + it { is_expected.to be_nil } end end diff --git a/ee/spec/requests/api/graphql/project/product_analytics/events_stored_spec.rb b/ee/spec/requests/api/graphql/project/product_analytics/events_stored_spec.rb index 605b886d001703d8ba19bd0ee3ac9df499fbf3b8..d095c75143e7dcd9642b0034158bdf51cc96bfe1 100644 --- a/ee/spec/requests/api/graphql/project/product_analytics/events_stored_spec.rb +++ b/ee/spec/requests/api/graphql/project/product_analytics/events_stored_spec.rb @@ -36,17 +36,18 @@ end context 'when project does not have product analytics enabled' do - it "returns zero for each months usage" do + it "returns nil for each months usage" do subject graphql_data.dig('project', 'productAnalyticsEventsStored').each do |event| - expect(event['count']).to be_zero + expect(event['count']).to be_nil end end end context 'when project does have product analytics enabled' do before do + project.project_setting.update!(product_analytics_instrumentation_key: 'abc-123') allow_next_instance_of(ProductAnalytics::Settings) do |instance| allow(instance).to receive(:enabled?).and_return(true) end