Add metrics around welcome notifications + their click events (#9239)

* Log to DataDog when a welcome notification is created

* Fix typo in Metrics::RecordDailyUsageWorker

* Add Metrics::RecordDailyNotificationsWorker to log notification counts to DataDog

* Add specs around DataDog logging for welcome notifications + click events

* Call Metrics::RecordDailyNotificationsWorker from within log_daily_usage_measurables task

* Use user_id instead of user, move notification title into tags
This commit is contained in:
Vaidehi Joshi 2020-07-10 03:30:45 -07:00 committed by GitHub
parent b3060bc475
commit 35b516a60d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 105 additions and 9 deletions

View file

@ -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

View file

@ -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

View file

@ -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"]

View file

@ -8,4 +8,5 @@ end
task log_daily_usage_measurables: :environment do
Metrics::RecordDailyUsageWorker.perform_async
Metrics::RecordDailyNotificationsWorker.perform_async
end

18
spec/factories/ahoy.rb Normal file
View file

@ -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

View file

@ -1,5 +0,0 @@
FactoryBot.define do
factory :ahoy_message, class: "Ahoy::Message" do
user
end
end

View file

@ -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

View file

@ -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