Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop linter for "enhanced" analytics events #10264

6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require:
- rubocop-rspec
- rubocop-performance
- ./lib/linters/analytics_event_name_linter.rb
- ./lib/linters/enhanced_idv_events_linter.rb
- ./lib/linters/localized_validation_message_linter.rb
- ./lib/linters/image_size_linter.rb
- ./lib/linters/mail_later_linter.rb
Expand Down Expand Up @@ -61,6 +62,11 @@ IdentityIdp/UrlOptionsLinter:
Exclude:
- 'spec/**/*.rb'

IdentityIdp/EnhancedIdvEventsLinter:
Enabled: true
Include:
- 'app/services/analytics_events.rb'

IdentityIdp/ErrorsAddLinter:
Enabled: true
Exclude:
Expand Down
314 changes: 296 additions & 18 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
Loading