Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/services/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def track_event(event, attributes = {})
event_properties: attributes.except(:user_id),
new_event: first_event_this_session?,
new_session_path: first_path_visit_this_session?,
new_session_success_state: first_success_state_this_session?,
success_state: success_state_token(event),
path: request&.path,
user_id: attributes[:user_id] || user.uuid,
locale: I18n.locale,
Expand All @@ -38,7 +40,11 @@ def track_event(event, attributes = {})
def update_session_events_and_paths_visited_for_analytics(event)
@session[:paths_visited] ||= {}
@session[:events] ||= {}
@session[:success_states] ||= {}
if request
token = success_state_token(event)
@session[:first_success_state] = !@session[:success_states].key?(token)
@session[:success_states][token] = true
@session[:first_path_visit] = !@session[:paths_visited].key?(request.path)
@session[:paths_visited][request.path] = true
end
Expand All @@ -50,6 +56,14 @@ def first_path_visit_this_session?
@session[:first_path_visit]
end

def first_success_state_this_session?
@session[:first_success_state]
end

def success_state_token(event)
"#{request&.env&.dig('REQUEST_METHOD')}|#{request&.path}|#{event}"
end

def first_event_this_session?
@session[:first_event]
end
Expand Down
4 changes: 4 additions & 0 deletions spec/controllers/openid_connect/user_info_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
'first_path_visit' => true,
'events' => { 'OpenID Connect: bearer token authentication' => true },
'first_event' => true,
'first_success_state' => true,
'success_states' => {
'POST|/api/openid_connect/userinfo|OpenID Connect: bearer token authentication' => true,
},
}
expect(request.session.to_h).to eq(session_hash)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/services/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
let(:current_user) { build_stubbed(:user, uuid: '123') }
let(:request) { FakeRequest.new }
let(:path) { 'fake_path' }
let(:success_state) { 'GET|fake_path|Trackable Event' }

subject(:analytics) do
Analytics.new(
Expand All @@ -48,6 +49,8 @@
git_sha: IdentityConfig::GIT_SHA,
git_branch: IdentityConfig::GIT_BRANCH,
new_session_path: true,
new_session_success_state: true,
success_state: success_state,
new_event: true,
path: path,
}
Expand All @@ -66,6 +69,8 @@
git_sha: IdentityConfig::GIT_SHA,
git_branch: IdentityConfig::GIT_BRANCH,
new_session_path: nil,
new_session_success_state: nil,
success_state: success_state,
new_event: nil,
path: path,
}
Expand All @@ -86,6 +91,8 @@
locale: I18n.locale,
git_sha: IdentityConfig::GIT_SHA,
git_branch: IdentityConfig::GIT_BRANCH,
new_session_success_state: true,
success_state: success_state,
new_session_path: true,
new_event: true,
path: path,
Expand Down Expand Up @@ -124,6 +131,8 @@
new_session_path: true,
new_event: true,
path: path,
new_session_success_state: true,
success_state: success_state,
}

expect(ahoy).to receive(:track).
Expand Down
4 changes: 4 additions & 0 deletions spec/support/fake_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ def cookies
def path
'fake_path'
end

def env
{ 'REQUEST_METHOD' => 'GET' }
end
end