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
303 changes: 287 additions & 16 deletions app/services/analytics_events.rb

Large diffs are not rendered by default.

59 changes: 52 additions & 7 deletions app/services/idv/analytics_events_enhancer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ module AnalyticsEventsEnhancer
idv_in_person_usps_proofing_results_job_unexpected_response
idv_in_person_usps_proofing_results_job_user_sent_to_fraud_review
idv_in_person_usps_request_enroll_exception
idv_intro_visit
idv_ipp_deactivated_for_never_visiting_post_office
idv_link_sent_capture_doc_polling_complete
idv_link_sent_capture_doc_polling_started
Expand All @@ -109,6 +108,20 @@ module AnalyticsEventsEnhancer
idv_warning_shown
].to_set.freeze

STANDARD_ARGUMENTS = %i[
proofing_components
active_profile_idv_level
pending_profile_idv_level
].freeze

METHODS_WITH_PROFILE_HISTORY = %i[
idv_doc_auth_verify_proofing_results
idv_intro_visit
idv_final
idv_please_call_visited
idv_start_over
].to_set.freeze

def self.included(_mod)
raise 'this mixin is intended to be prepended, not included'
end
Expand All @@ -117,24 +130,56 @@ def self.prepended(mod)
mod.instance_methods.each do |method_name|
if should_enhance_method?(method_name)
mod.define_method method_name do |**kwargs|
super(**kwargs, **common_analytics_attributes)
super(**kwargs, **analytics_attributes(method_name))
end
end
end
end

def self.should_enhance_method?(method_name)
return false if IGNORED_METHODS.include?(method_name)

method_name.start_with?('idv_')
end

def self.extra_args_for_method(method_name)
return [] unless should_enhance_method?(method_name)

args = STANDARD_ARGUMENTS

if METHODS_WITH_PROFILE_HISTORY.include?(method_name)
args = [
*args,
:profile_history,
]
end

args
end

private

def common_analytics_attributes
{
proofing_components: proofing_components,
}.compact
def analytics_attributes(method_name)
AnalyticsEventsEnhancer.extra_args_for_method(method_name).
index_with do |arg_name|
send(arg_name.to_s).presence
end.
compact
end

def active_profile_idv_level
user&.respond_to?(:active_profile) && user&.active_profile&.idv_level
end

def pending_profile_idv_level
user&.respond_to?(:pending_profile) && user&.pending_profile&.idv_level
end

def profile_history
return if !user&.respond_to?(:profiles)

(user&.profiles || []).
sort_by { |profile| profile.created_at }.
map { |profile| ProfileLogging.new(profile) }
end

def proofing_components
Expand Down
22 changes: 22 additions & 0 deletions app/services/idv/profile_logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Idv
ProfileLogging = Struct.new(:profile) do
def as_json
profile.slice(
%i[
id
active
idv_level
created_at
verified_at
activated_at
in_person_verification_pending_at
gpo_verification_pending_at
fraud_review_pending_at
fraud_rejection_at
fraud_pending_reason
deactivation_reason
],
).compact
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

get :show

expect(@analytics).to have_logged_event(
'IdV: letter enqueued visited',
proofing_components: nil,
)
expect(@analytics).to have_logged_event('IdV: letter enqueued visited')
end

it 'renders the show template' do
Expand Down
42 changes: 23 additions & 19 deletions spec/controllers/idv/by_mail/request_letter_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@

expect(@analytics).to have_logged_event(
'IdV: USPS address letter requested',
resend: false,
phone_step_attempts: 1,
first_letter_requested_at: nil,
hours_since_first_letter: 0,
proofing_components: nil,
**ab_test_args,
hash_including(
resend: false,
phone_step_attempts: 1,
first_letter_requested_at: nil,
hours_since_first_letter: 0,
**ab_test_args,
),
)
end

Expand Down Expand Up @@ -222,23 +223,26 @@

expect(@analytics).to have_logged_event(
'IdV: USPS address letter requested',
resend: true,
phone_step_attempts: 1,
first_letter_requested_at: pending_profile.gpo_verification_pending_at,
hours_since_first_letter: 24,
proofing_components: nil,
**ab_test_args,
hash_including(
resend: true,
phone_step_attempts: 1,
first_letter_requested_at: pending_profile.gpo_verification_pending_at,
hours_since_first_letter: 24,
**ab_test_args,
),
)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding

active_profile_idv_level: nil,
pending_profile_idv_level: nil,
proofing_components: nil,

here, I switched it (and several other specs) to use hash_including. My feeling is that these tests aren't really testing these extra analytics arguments--those should be covered by a combination of the Idv::AnalyticsEventsEnhancer spec and the analytics feature spec.


expect(@analytics).to have_logged_event(
'IdV: USPS address letter enqueued',
resend: true,
first_letter_requested_at: pending_profile.gpo_verification_pending_at,
hours_since_first_letter: 24,
enqueued_at: Time.zone.now,
phone_step_attempts: 1,
proofing_components: nil,
**ab_test_args,
hash_including(
resend: true,
first_letter_requested_at: pending_profile.gpo_verification_pending_at,
hours_since_first_letter: 24,
enqueued_at: Time.zone.now,
phone_step_attempts: 1,
proofing_components: nil,
**ab_test_args,
),
)
end

Expand Down
43 changes: 22 additions & 21 deletions spec/controllers/idv/cancellations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

expect(@analytics).to have_logged_event(
'IdV: cancellation visited',
request_came_from: 'no referer',
step: nil,
proofing_components: nil,
hash_including(
request_came_from: 'no referer',
step: nil,
),
)
end

Expand All @@ -37,9 +38,10 @@

expect(@analytics).to have_logged_event(
'IdV: cancellation visited',
request_came_from: 'users/sessions#new',
step: nil,
proofing_components: nil,
hash_including(
request_came_from: 'users/sessions#new',
step: nil,
),
)
end

Expand All @@ -51,9 +53,10 @@

expect(@analytics).to have_logged_event(
'IdV: cancellation visited',
request_came_from: 'no referer',
step: 'first',
proofing_components: nil,
hash_including(
request_came_from: 'no referer',
step: 'first',
),
)
end

Expand Down Expand Up @@ -115,11 +118,9 @@

expect(@analytics).to have_logged_event(
'IdV: cancellation go back',
step: 'first',
proofing_components: nil,
cancelled_enrollment: nil,
enrollment_code: nil,
enrollment_id: nil,
hash_including(
step: 'first',
),
)
end

Expand All @@ -137,11 +138,12 @@

expect(@analytics).to have_logged_event(
'IdV: cancellation go back',
step: 'barcode',
proofing_components: nil,
cancelled_enrollment: false,
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
hash_including(
step: 'barcode',
cancelled_enrollment: false,
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
),
)
end
end
Expand Down Expand Up @@ -172,8 +174,7 @@

expect(@analytics).to have_logged_event(
'IdV: cancellation confirmed',
step: 'first',
proofing_components: nil,
hash_including(step: 'first'),
)
end

Expand Down
Loading