diff --git a/app/services/notifications/welcome_notification/send.rb b/app/services/notifications/welcome_notification/send.rb index a97b9765b..5a34c7ecc 100644 --- a/app/services/notifications/welcome_notification/send.rb +++ b/app/services/notifications/welcome_notification/send.rb @@ -30,11 +30,20 @@ module Notifications action: welcome_broadcast.type_of, json_data: json_data, ) + + log_to_datadog end private attr_reader :receiver_id, :welcome_broadcast + + def log_to_datadog + DatadogStatsClient.increment( + "notifications.welcome", + tags: ["user_id:#{receiver_id}", "title:#{welcome_broadcast.title}"], + ) + end end end end diff --git a/app/workers/metrics/record_daily_notifications_worker.rb b/app/workers/metrics/record_daily_notifications_worker.rb new file mode 100644 index 000000000..7eb1f7be8 --- /dev/null +++ b/app/workers/metrics/record_daily_notifications_worker.rb @@ -0,0 +1,34 @@ +module Metrics + class RecordDailyNotificationsWorker + include Sidekiq::Worker + sidekiq_options queue: :low_priority, retry: 10 + + EVENT_TITLES = %w[ + welcome_notification_set_up_profile + welcome_notification_welcome_thread + welcome_notification_customize_feed + welcome_notification_twitter_connect + welcome_notification_github_connect + welcome_notification_customize_experience + welcome_notification_discuss_and_ask + welcome_notification_download_app + welcome_notification_ask_question + welcome_notification_start_discussion + ].freeze + + def perform + # Welcome Notification click events created in the past day, logged by title. + EVENT_TITLES.each do |title| + event = Ahoy::Event.where(name: "Clicked Welcome Notification"). + where("time > ?", 1.day.ago). + where("properties->>'title' = ?", title) + + DatadogStatsClient.count( + "ahoy_events", + event.size, + tags: ["title:#{title}"], + ) + end + end + end +end diff --git a/app/workers/metrics/record_daily_usage_worker.rb b/app/workers/metrics/record_daily_usage_worker.rb index e5b654f0f..94f383173 100644 --- a/app/workers/metrics/record_daily_usage_worker.rb +++ b/app/workers/metrics/record_daily_usage_worker.rb @@ -35,8 +35,8 @@ module Metrics ) # Total negative reactions in the past 24 hours - nagative_reactions_past_24h = Reaction.where("points < 0").where("created_at > ?", 1.day.ago).size - DatadogStatsClient.count("reactions.negative_past_24h", nagative_reactions_past_24h, tags: ["resource:reactions"]) + negative_reactions_past_24h = Reaction.where("points < 0").where("created_at > ?", 1.day.ago).size + DatadogStatsClient.count("reactions.negative_past_24h", negative_reactions_past_24h, tags: ["resource:reactions"]) # Total abuse (etc.) reports in the past 24 hours categories = ["spam", "other", "rude or vulgar", "harassment"] diff --git a/lib/tasks/metrics.rake b/lib/tasks/metrics.rake index d957854eb..22f051caf 100644 --- a/lib/tasks/metrics.rake +++ b/lib/tasks/metrics.rake @@ -8,4 +8,5 @@ end task log_daily_usage_measurables: :environment do Metrics::RecordDailyUsageWorker.perform_async + Metrics::RecordDailyNotificationsWorker.perform_async end diff --git a/spec/factories/ahoy.rb b/spec/factories/ahoy.rb new file mode 100644 index 000000000..4dc95f619 --- /dev/null +++ b/spec/factories/ahoy.rb @@ -0,0 +1,18 @@ +FactoryBot.define do + factory :ahoy_message, class: "Ahoy::Message" do + user + end + + factory :ahoy_visit, class: "Ahoy::Visit" do + user + started_at { Timecop.freeze(Time.zone.now) } + end + + factory :ahoy_event, class: "Ahoy::Event" do + user + visit { create(:ahoy_visit, user: user) } # Ahoy::Events require an Ahoy::Visit + time { Timecop.freeze(Time.zone.now) } + name { "Clicked Welcome Notification" } + properties { { title: "welcome_notification_welcome_thread" } } + end +end diff --git a/spec/factories/ahoy_message.rb b/spec/factories/ahoy_message.rb deleted file mode 100644 index dc1d54652..000000000 --- a/spec/factories/ahoy_message.rb +++ /dev/null @@ -1,5 +0,0 @@ -FactoryBot.define do - factory :ahoy_message, class: "Ahoy::Message" do - user - end -end diff --git a/spec/services/notifications/welcome_notification/send_spec.rb b/spec/services/notifications/welcome_notification/send_spec.rb index dc5873234..c108e3635 100644 --- a/spec/services/notifications/welcome_notification/send_spec.rb +++ b/spec/services/notifications/welcome_notification/send_spec.rb @@ -2,18 +2,31 @@ require "rails_helper" RSpec.describe Notifications::WelcomeNotification::Send, type: :service do describe "::call" do + let!(:welcome_broadcast) { create(:set_up_profile_broadcast) } + before do allow(User).to receive(:mascot_account).and_return(create(:user)) + allow(DatadogStatsClient).to receive(:increment) end it "creates a new welcome notification", :aggregate_failures do - welcome_broadcast = create(:set_up_profile_broadcast) - welcome_notification = described_class.call(create(:user).id, welcome_broadcast) + expect(Notification.find_by(notifiable_id: welcome_broadcast.id)).to be(nil) + described_class.call(create(:user).id, welcome_broadcast) + + welcome_notification = Notification.find_by(notifiable_id: welcome_broadcast.id) expect(welcome_notification).to be_kind_of(Notification) expect(welcome_notification.notifiable_type).to eq "Broadcast" expect(welcome_notification.action).to eq "Welcome" expect(welcome_notification.json_data["broadcast"]["processed_html"]).to eq welcome_broadcast.processed_html end + + it "logs to DataDog" do + described_class.call(create(:user).id, welcome_broadcast) + welcome_notification = Notification.find_by(notifiable_id: welcome_broadcast.id) + tags = ["user_id:#{welcome_notification.user_id}", "title:#{welcome_notification.notifiable.title}"] + + expect(DatadogStatsClient).to have_received(:increment).with("notifications.welcome", tags: tags) + end end end diff --git a/spec/workers/metrics/record_daily_notifications_worker_spec.rb b/spec/workers/metrics/record_daily_notifications_worker_spec.rb new file mode 100644 index 000000000..7230da9c7 --- /dev/null +++ b/spec/workers/metrics/record_daily_notifications_worker_spec.rb @@ -0,0 +1,26 @@ +require "rails_helper" + +RSpec.describe Metrics::RecordDailyNotificationsWorker, type: :worker do + include_examples "#enqueues_on_correct_queue", "low_priority", 1 + + describe "#perform" do + let(:user) { create(:user) } + let!(:ahoy_event) { create(:ahoy_event) } + let(:event_title_count) { Metrics::RecordDailyNotificationsWorker::EVENT_TITLES.count } + + before do + allow(DatadogStatsClient).to receive(:count) + ahoy_event + described_class.new.perform + end + + it "logs welcome notification click events created in the past day" do + expect(DatadogStatsClient).to have_received(:count).exactly(event_title_count).times + expect( + DatadogStatsClient, + ).to have_received(:count). + with("ahoy_events", 1, { tags: ["title:welcome_notification_welcome_thread"] }). + at_least(1) + end + end +end