From a68d72eece2ff9b985ad360ee9645a3d9dc0dc57 Mon Sep 17 00:00:00 2001 From: Tom Dooner Date: Tue, 20 Aug 2024 16:53:52 -0700 Subject: [PATCH] Track seconds between invitation sent, flow start, and completion This commit adds metadata tracking for the seconds in two intervals: 1. invitation sent and flow start, and 2. flow start and flow completion Although, these can be reconstituted later from the database timestamps, it might be best to just send them to NewRelic so we can put them on a dashboard easily. --- app/app/controllers/cbv/base_controller.rb | 3 +- .../controllers/cbv/summaries_controller.rb | 3 +- .../cbv/entries_controller_spec.rb | 28 ++++++++++++++++++- .../cbv/summaries_controller_spec.rb | 12 ++++++-- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/app/controllers/cbv/base_controller.rb b/app/app/controllers/cbv/base_controller.rb index dd79c5c2..44f8bdc9 100644 --- a/app/app/controllers/cbv/base_controller.rb +++ b/app/app/controllers/cbv/base_controller.rb @@ -24,7 +24,8 @@ def set_cbv_flow timestamp: Time.now.to_i, invitation_id: invitation.id, cbv_flow_id: @cbv_flow.id, - site_id: @cbv_flow.site_id + site_id: @cbv_flow.site_id, + seconds_since_invitation: (Time.now - invitation.created_at).to_i }) if @cbv_flow.pinwheel_accounts.any? diff --git a/app/app/controllers/cbv/summaries_controller.rb b/app/app/controllers/cbv/summaries_controller.rb index e31d44c2..f2172a2e 100644 --- a/app/app/controllers/cbv/summaries_controller.rb +++ b/app/app/controllers/cbv/summaries_controller.rb @@ -85,7 +85,8 @@ def track_transmitted_event(cbv_flow, payments) account_count: payments.map { |p| p[:account_id] }.uniq.count, paystub_count: payments.count, account_count_with_additional_information: - cbv_flow.additional_information.values.count { |info| info["comment"].present? } + cbv_flow.additional_information.values.count { |info| info["comment"].present? }, + flow_started_seconds_ago: (Time.now - cbv_flow.created_at).to_i }) rescue => ex Rails.logger.error "Failed to track NewRelic event: #{ex.message}" diff --git a/app/spec/controllers/cbv/entries_controller_spec.rb b/app/spec/controllers/cbv/entries_controller_spec.rb index b2496185..939a6684 100644 --- a/app/spec/controllers/cbv/entries_controller_spec.rb +++ b/app/spec/controllers/cbv/entries_controller_spec.rb @@ -11,7 +11,18 @@ end context "when following a link from a flow invitation" do - let(:invitation) { create(:cbv_flow_invitation, case_number: "ABC1234") } + let(:seconds_since_invitation) { 300 } + let(:invitation) do + create( + :cbv_flow_invitation, + case_number: "ABC1234", + created_at: seconds_since_invitation.seconds.ago + ) + end + + around do |ex| + Timecop.freeze(&ex) + end it "renders properly" do get :show, params: { token: invitation.auth_token } @@ -32,6 +43,21 @@ ) end + it "sends a NewRelic event with metadata" do + allow(NewRelicEventTracker).to receive(:track) + + get :show, params: { token: invitation.auth_token } + cbv_flow = CbvFlow.find(session[:cbv_flow_id]) + + expect(NewRelicEventTracker).to have_received(:track).with("ClickedCBVInvitationLink", { + timestamp: be_a(Integer), + invitation_id: invitation.id, + cbv_flow_id: cbv_flow.id, + site_id: invitation.site_id, + seconds_since_invitation: seconds_since_invitation + }) + end + context "when returning to an already-visited flow invitation" do let(:existing_cbv_flow) { create(:cbv_flow, cbv_flow_invitation: invitation) } diff --git a/app/spec/controllers/cbv/summaries_controller_spec.rb b/app/spec/controllers/cbv/summaries_controller_spec.rb index d9743ed3..28a13ae6 100644 --- a/app/spec/controllers/cbv/summaries_controller_spec.rb +++ b/app/spec/controllers/cbv/summaries_controller_spec.rb @@ -3,13 +3,20 @@ RSpec.describe Cbv::SummariesController do include PinwheelApiHelper - let(:cbv_flow) { create(:cbv_flow, case_number: "ABC1234", pinwheel_token_id: "abc-def-ghi") } + let(:flow_started_seconds_ago) { 300 } + let(:cbv_flow) do + create(:cbv_flow, case_number: "ABC1234", pinwheel_token_id: "abc-def-ghi", created_at: flow_started_seconds_ago.seconds.ago) + end let(:cbv_flow_invitation) { cbv_flow.cbv_flow_invitation } before do session[:cbv_flow_invitation] = cbv_flow_invitation end + around do |ex| + Timecop.freeze(&ex) + end + describe "#show" do before do cbv_flow_invitation.update(snap_application_date: Date.parse('2024-06-18')) @@ -150,7 +157,8 @@ cbv_flow_id: cbv_flow.id, account_count: 1, paystub_count: 1, - account_count_with_additional_information: 0 + account_count_with_additional_information: 0, + flow_started_seconds_ago: flow_started_seconds_ago }) end end