Skip to content

Commit

Permalink
Merge branch 'main' into support-chat
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo authored Feb 11, 2025
2 parents 61302a9 + 78f8c44 commit 2e39003
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 204 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ gem 'rack-cors'
# Data visualization and query
gem 'blazer', '< 3.0'

# Product analysis, A/B testing
gem 'posthog-ruby'

group :development, :production do
# Get env variables from .env file
gem 'dotenv-rails'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.8)
posthog-ruby (2.5.1)
concurrent-ruby (~> 1)
premailer (1.27.0)
addressable
css_parser (>= 1.19.0)
Expand Down Expand Up @@ -871,6 +873,7 @@ DEPENDENCIES
parallel_tests
pattern-library!
pg
posthog-ruby
premailer-rails
psych (< 4)
puma
Expand Down
34 changes: 33 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ApplicationController < ActionController::Base
before_action :authenticate_user!
before_action :complete_signup_profile
before_action :check_if_password_expired
before_action :set_sentry_user

fine_print_require :general_terms_of_use, :privacy_policy, unless: :disable_fine_print

Expand All @@ -14,7 +15,7 @@ def disable_fine_print
end

def check_if_admin
return true if !Rails.env.production?
return true if Rails.env.test?
is_admin?
end

Expand All @@ -35,6 +36,37 @@ def return_url_specified_and_allowed?

include Lev::HandleWith

def set_sentry_user
return if current_user.is_anonymous?
Sentry.set_user(uuid: current_user.uuid)
end

def log_posthog(user, event)
return if user.nil? or user.is_anonymous? or Rails.env.test?
begin
OXPosthog.posthog.capture({
distinct_id: user.uuid,
event: event,
properties: {
'$set': { email: user.email_addresses&.first&.value,
name: user.full_name,
uuid: user.uuid,
role: user.role,
faculty_status: user.faculty_status,
school: user.school&.id,
recent_authentication_provider: user.authentications&.last&.provider,
authentication_method_count: user.authentications&.count,
salesforce_contact_id: user.salesforce_contact_id,
salesforce_lead_id: user.salesforce_lead_id,
}
}
})
rescue StandardError => e
Sentry.capture_exception(e)
return
end
end

respond_to :html

protected #################
Expand Down
1 change: 1 addition & 0 deletions app/controllers/external_user_credentials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def create
CreateExternalUserCredentials,
success: -> {
security_log :student_created_password
log_posthog(current_user, "user_created_with_external_credentials")
redirect_to @return_to
},
failure: -> {
Expand Down
13 changes: 11 additions & 2 deletions app/controllers/newflow/educator_signup_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ def educator_signup
client_app: get_client_app,
is_BRI_book: is_BRI_book_adopter?,
success: lambda {
save_unverified_user(@handler_result.outputs.user.id)
security_log(:educator_began_signup, { user: @handler_result.outputs.user })
@user = @handler_result.outputs.user
save_unverified_user(@user.id)
security_log(:educator_began_signup, { user: @user })
log_posthog(@user, 'educator_started_signup')
clear_cache_BRI_marketing
redirect_to(educator_email_verification_form_path)
},
Expand Down Expand Up @@ -91,20 +93,23 @@ def educator_verify_email_by_pin
clear_unverified_user
sign_in!(@handler_result.outputs.user)
security_log(:educator_verified_email, email:@email)
log_posthog(@handler_result.outputs.user, 'educator_verified_email')
redirect_to(educator_sheerid_form_path)
},
failure: lambda {
@total_steps = 4
@first_name = unverified_user.first_name
@email = unverified_user.email_addresses.first.value
security_log(:educator_verify_email_failed, email: @email)
log_posthog(unverified_user, "educator_verified_email_failed")
render(:educator_email_verification_form)
}
)
end

def educator_sheerid_form
@sheerid_url = generate_sheer_id_url(user: current_user)
log_posthog(current_user, 'educator_view_sheer_id_form')
security_log(:user_viewed_sheerid_form, user: current_user)
end

Expand Down Expand Up @@ -135,6 +140,7 @@ def sheerid_webhook
def educator_profile_form
@book_titles = book_data.titles
security_log(:user_viewed_profile_form, form_name: action_name, user: current_user)
log_posthog(current_user, 'educator_viewed_profile_form')
end

def educator_complete_profile
Expand All @@ -143,6 +149,7 @@ def educator_complete_profile
user: current_user,
success: lambda {
user = @handler_result.outputs.user
log_posthog(user, 'educator_complete_profile')
security_log(:user_profile_complete, { user: user })
clear_incomplete_educator

Expand All @@ -155,6 +162,7 @@ def educator_complete_profile
failure: lambda {
@book_titles = book_data.titles
security_log(:educator_sign_up_failed, user: current_user, reason: @handler_result.errors)
log_posthog(current_user, 'educator_complete_profile_failed')
if @handler_result.outputs.is_on_cs_form
redirect_to(educator_cs_verification_form_url, alert: "Please check your input and try again. Email address and School Name are required fields.")
else
Expand All @@ -167,6 +175,7 @@ def educator_complete_profile
def educator_pending_cs_verification
security_log(:user_sent_to_cs_for_review, user: current_user)
@email_address = current_user.email_addresses.last&.value
log_posthog(current_user, 'educator_sent_to_cs_for_review')
end

private #################
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/newflow/login_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def login
success: lambda {
clear_signup_state
user = @handler_result.outputs.user
Sentry.set_user(id: user.id) if Rails.env.production?

if user.unverified?
save_unverified_user(user.id)
Expand All @@ -33,6 +32,7 @@ def login
end

sign_in!(user, security_log_data: {'email': @handler_result.outputs.email})
log_posthog(user, 'user_logged_in')

if current_user.student? || !current_user.is_newflow? || (edu_newflow_activated? && decorated_user.can_do?('redirect_back_upon_login'))
did_user_sign_recent_privacy_notice? ? redirect_back : redirect_to_sign_privacy_notice
Expand All @@ -57,6 +57,7 @@ def login
end

def logout
log_posthog(current_user, 'user_logged_out')
sign_out!
Sentry.set_user({}) if Rails.env.production?
redirect_back(fallback_location: newflow_login_path)
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/newflow/password_management_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class PasswordManagementController < BaseController

def forgot_password_form
@email = login_failed_email
user = ContactInfo.find_by(value: @email)&.user
unless user.nil?
log_posthog(user, 'user_forgot_password')
end
end

def send_reset_password_email
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/newflow/signup_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ def verify_email_by_code

if user.student?
security_log(:student_verified_email, {user: user, message: "Student verified email."})
log_posthog(user, 'student_verified_email')
redirect_to signup_done_path
else
security_log(:educator_verified_email, {user: user, message: "Educator verified email."})
log_posthog(user, 'educator_verified_email')
redirect_to(educator_sheerid_form_path)
end
},
Expand All @@ -35,6 +37,7 @@ def verify_email_by_code

def signup_done
security_log(:user_viewed_signup_form, form_name: action_name)
log_posthog(current_user, 'user_signup_done')
@first_name = current_user.first_name
@email_address = current_user.email_addresses.first&.value
end
Expand All @@ -49,6 +52,7 @@ def skip_signup_done_for_tutor_users

def exit_newflow_signup_if_logged_in
if signed_in?
log_posthog(current_user, 'user_redirected_because_signed_in')
redirect_back(fallback_location: profile_newflow_path(request.query_parameters))
end
end
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/newflow/social_auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ def oauth_callback
@last_name = user.last_name
@email = @handler_result.outputs.email
security_log(:student_social_sign_up, user: user, authentication_id: authentication.id)
log_posthog(user, "student_signup_#{authentication.provider}")
# must confirm their social info on signup
render :confirm_social_info_form and return # TODO: if possible, update the route/path to reflect that this page is being rendered
end

sign_in!(user)
log_posthog(user, "user_logged_in_with_#{authentication.provider}")
security_log(:authenticated_with_social, user: user, authentication_id: authentication.id)
redirect_back(fallback_location: profile_newflow_path)

Expand All @@ -54,6 +56,7 @@ def oauth_callback

code = @handler_result.errors.first.code
authentication = @handler_result.outputs.authentication

case code
when :should_redirect_to_signup
redirect_to(
Expand All @@ -63,7 +66,7 @@ def oauth_callback
sign_up: view_context.link_to(I18n.t(:"login_signup_form.sign_up"), newflow_signup_path)
)
)
when :authentication_taken || :taken
when :authentication_taken || 'taken'
security_log(:authentication_transfer_failed, authentication_id: authentication.id)
redirect_to(error_path(is_external, code), alert: I18n.t(:"controllers.sessions.sign_in_option_already_used"))
when :email_already_in_use
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/newflow/student_signup_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def student_signup
contracts_required: !contracts_not_required,
client_app: get_client_app,
success: lambda {
save_unverified_user(@handler_result.outputs.user.id)
security_log(:student_signed_up, { user: @handler_result.outputs.user })
user = @handler_result.outputs.user
save_unverified_user(user.id)
security_log(:student_signed_up, { user: user })
log_posthog(user, "student_started_signup")
redirect_to student_email_verification_form_path
},
failure: lambda {
Expand Down Expand Up @@ -65,6 +67,7 @@ def student_verify_email_by_pin
user = @handler_result.outputs.user
sign_in!(user)
security_log(:student_verified_email)
log_posthog(user, "student_verified_email")
redirect_to(signup_done_path)
},
failure: lambda {
Expand Down
5 changes: 5 additions & 0 deletions app/routines/newflow/create_or_update_salesforce_lead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def exec(user:)
lead = OpenStax::Salesforce::Remote::Lead.new(email: user.best_email_address_for_salesforce)
end

if lead.nil?
Sentry.capture_message("Lead for user not found #{user.uuid} not found", level: :error)
return
end

lead.first_name = user.first_name
lead.last_name = user.last_name
lead.phone = user.phone_number
Expand Down
96 changes: 0 additions & 96 deletions app/views/layouts/_google_analytics.html.erb

This file was deleted.

8 changes: 4 additions & 4 deletions app/views/layouts/_google_tag_manager.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% if Rails.env.production? and Settings::GoogleAnalytics.send_google_analytics == true and !Settings::GoogleAnalytics.google_tag_manager_code.blank? %>
<script>
//Code from Google
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','<%= Settings::GoogleAnalytics.google_tag_manager_code %>');
</script>
<% end %>
Loading

0 comments on commit 2e39003

Please sign in to comment.