Skip to content

Commit

Permalink
Add app_differentiator field to analytics events (#11262)
Browse files Browse the repository at this point in the history
* changelog: Internal, Analytics, Add app_differentiator field to events
  • Loading branch information
Sgtpluck authored Sep 27, 2024
1 parent 3d42401 commit de3f80f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/services/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,28 @@ 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

{ 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)
Expand Down
78 changes: 78 additions & 0 deletions spec/services/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit de3f80f

Please sign in to comment.