diff --git a/app/controllers/users/phones_controller.rb b/app/controllers/users/phones_controller.rb index db35275226a..1efdd37f611 100644 --- a/app/controllers/users/phones_controller.rb +++ b/app/controllers/users/phones_controller.rb @@ -13,11 +13,13 @@ class PhonesController < ApplicationController def add user_session[:phone_id] = nil @new_phone_form = NewPhoneForm.new(user: current_user, analytics: analytics) + analytics.add_phone_setup_visit end def create @new_phone_form = NewPhoneForm.new(user: current_user, analytics: analytics) result = @new_phone_form.submit(user_params) + analytics.multi_factor_auth_phone_setup(**result.to_h) if result.success? confirm_phone elsif recoverable_recaptcha_error?(result) diff --git a/app/services/analytics_events.rb b/app/services/analytics_events.rb index 7d1ccb3b50b..8e30398a7a4 100644 --- a/app/services/analytics_events.rb +++ b/app/services/analytics_events.rb @@ -2817,6 +2817,13 @@ def telephony_otp_sent( ) end + # Tracks When users visit the add phone page + def add_phone_setup_visit + track_event( + 'Phone Setup Visited', + ) + end + # @param [Boolean] success # @param [Hash] errors # @param [String] sign_up_mfa_priority_bucket diff --git a/spec/controllers/users/phones_controller_spec.rb b/spec/controllers/users/phones_controller_spec.rb index 3f8a1f18122..f9d7678c67e 100644 --- a/spec/controllers/users/phones_controller_spec.rb +++ b/spec/controllers/users/phones_controller_spec.rb @@ -16,6 +16,12 @@ expect(response).to render_template(:add) expect(response.request.flash[:alert]).to be_nil end + + it 'tracks analytics' do + expect(@analytics).to receive(:track_event). + with('Phone Setup Visited') + get :add + end end context 'user exceeds phone number limit' do @@ -104,5 +110,132 @@ expect(response).to render_template('users/phone_setup/spam_protection') end end + + context 'invalid number' do + it 'tracks an event when the number is invalid' do + sign_in(user) + + stub_analytics + result = { + success: false, + errors: { + phone: [ + t('errors.messages.improbable_phone'), + t( + 'two_factor_authentication.otp_delivery_preference.voice_unsupported', + location: '', + ), + ], + }, + error_details: { + phone: [ + :improbable_phone, + t( + 'two_factor_authentication.otp_delivery_preference.voice_unsupported', + location: '', + ), + ], + }, + otp_delivery_preference: 'sms', + area_code: nil, + carrier: 'Test Mobile Carrier', + country_code: nil, + phone_type: :mobile, + types: [], + pii_like_keypaths: [[:errors, :phone], [:error_details, :phone]], + } + + expect(@analytics).to receive(:track_event). + with('Multi-Factor Authentication: phone setup', result) + + post :create, params: { + new_phone_form: { + phone: '703-555-010', + international_code: 'US', + }, + } + + expect(response).to render_template(:add) + end + end + + context 'with SMS' do + it 'prompts to confirm the number' do + sign_in(user) + + stub_analytics + + result = { + success: true, + errors: {}, + otp_delivery_preference: 'sms', + area_code: '703', + carrier: 'Test Mobile Carrier', + country_code: 'US', + phone_type: :mobile, + types: [:fixed_or_mobile], + pii_like_keypaths: [[:errors, :phone], [:error_details, :phone]], + } + + expect(@analytics).to receive(:track_event). + with('Multi-Factor Authentication: phone setup', result) + + post( + :create, + params: { + new_phone_form: { phone: '703-555-0100', + international_code: 'US' }, + }, + ) + + expect(response).to redirect_to( + otp_send_path( + otp_delivery_selection_form: { otp_delivery_preference: 'sms', + otp_make_default_number: false }, + ), + ) + + expect(subject.user_session[:context]).to eq 'confirmation' + end + end + + context 'without selection' do + it 'prompts to confirm via SMS by default' do + sign_in(user) + + stub_analytics + result = { + success: true, + errors: {}, + otp_delivery_preference: 'sms', + area_code: '703', + carrier: 'Test Mobile Carrier', + country_code: 'US', + phone_type: :mobile, + types: [:fixed_or_mobile], + pii_like_keypaths: [[:errors, :phone], [:error_details, :phone]], + } + + expect(@analytics).to receive(:track_event). + with('Multi-Factor Authentication: phone setup', result) + + patch( + :create, + params: { + new_phone_form: { phone: '703-555-0100', + international_code: 'US' }, + }, + ) + + expect(response).to redirect_to( + otp_send_path( + otp_delivery_selection_form: { otp_delivery_preference: 'sms', + otp_make_default_number: false }, + ), + ) + + expect(subject.user_session[:context]).to eq 'confirmation' + end + end end end