diff --git a/app/services/analytics.rb b/app/services/analytics.rb index 8e7c59cf5c4..378a89712e7 100644 --- a/app/services/analytics.rb +++ b/app/services/analytics.rb @@ -134,6 +134,11 @@ def sp_request_attributes [v.name.sub('http://idmanagement.gov/ns/assurance/', ''), true] end.to_h attributes.reject! { |_key, value| value == false } + + if differentiator.present? + attributes[:app_differentiator] = differentiator + end + attributes.transform_keys! do |key| key.to_s.chomp('?').to_sym end @@ -141,6 +146,16 @@ def sp_request_attributes { sp_request: attributes } end + def differentiator + return @differentiator if defined?(@differentiator) + @differentiator ||= begin + sp_request_url = session&.dig(:sp, :request_url) + return nil if sp_request_url.blank? + + UriService.params(sp_request_url)['login_gov_app_differentiator'] + end + end + def resolved_authn_context_result return nil if sp.nil? || session[:sp].blank? return @resolved_authn_context_result if defined?(@resolved_authn_context_result) diff --git a/spec/services/analytics_spec.rb b/spec/services/analytics_spec.rb index 07b0dd5dc02..402e75cda4a 100644 --- a/spec/services/analytics_spec.rb +++ b/spec/services/analytics_spec.rb @@ -358,4 +358,82 @@ end end end + + context 'with an SP request_url saved in the session' do + context 'no request_url' do + let(:session) { { sp: { acr_values: Saml::Idp::Constants::IAL1_AUTHN_CONTEXT_CLASSREF } } } + + let(:expected_attributes) do + { + sp_request: { + component_values: { 'ial/1' => true }, + component_separator: ' ', + }, + } + end + + it 'includes the sp_request' do + expect(ahoy).to receive(:track). + with('Trackable Event', hash_including(expected_attributes)) + + analytics.track_event('Trackable Event') + end + end + + context 'a request_url without login_gov_app_differentiator ' do + let(:session) do + { + sp: { + acr_values: Saml::Idp::Constants::IAL1_AUTHN_CONTEXT_CLASSREF, + request_url: 'http://localhost:3000/openid_connect/authorize?whatever=something_else', + }, + } + end + + let(:expected_attributes) do + { + sp_request: { + component_values: { 'ial/1' => true }, + component_separator: ' ', + }, + } + end + + it 'includes the sp_request' do + expect(ahoy).to receive(:track). + with('Trackable Event', hash_including(expected_attributes)) + + analytics.track_event('Trackable Event') + end + end + + context 'a request_url with login_gov_app_differentiator ' do + let(:session) do + { + sp: { + acr_values: Saml::Idp::Constants::IAL1_AUTHN_CONTEXT_CLASSREF, + request_url: + 'http://localhost:3000/openid_connect/authorize?login_gov_app_differentiator=NY', + }, + } + end + + let(:expected_attributes) do + { + sp_request: { + component_values: { 'ial/1' => true }, + component_separator: ' ', + app_differentiator: 'NY', + }, + } + end + + it 'includes the sp_request' do + expect(ahoy).to receive(:track). + with('Trackable Event', hash_including(expected_attributes)) + + analytics.track_event('Trackable Event') + end + end + end end