1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/notifications/message_center_stats_collector.h"
9 #include "base/metrics/histogram.h"
10 #include "content/public/browser/user_metrics.h"
11 #include "ui/message_center/message_center.h"
13 MessageCenterStatsCollector::NotificationStats::NotificationStats() {}
15 MessageCenterStatsCollector::NotificationStats::NotificationStats(
16 const std::string
& id
) : id_(id
) {
17 for (size_t i
= 0; i
< NOTIFICATION_ACTION_COUNT
; i
++) {
22 MessageCenterStatsCollector::NotificationStats::~NotificationStats() {}
24 void MessageCenterStatsCollector::NotificationStats::CollectAction(
25 NotificationActionType type
) {
28 UMA_HISTOGRAM_ENUMERATION("Notifications.Actions",
30 NOTIFICATION_ACTION_COUNT
);
31 actions_
[type
] = true;
34 void MessageCenterStatsCollector::NotificationStats::RecordAggregateStats() {
37 for (size_t i
= 0; i
< NOTIFICATION_ACTION_COUNT
; i
++) {
40 UMA_HISTOGRAM_ENUMERATION("Notifications.PerNotificationActions",
42 NOTIFICATION_ACTION_COUNT
);
46 MessageCenterStatsCollector::MessageCenterStatsCollector(
47 message_center::MessageCenter
* message_center
)
48 : message_center_(message_center
) {
49 message_center_
->AddObserver(this);
52 MessageCenterStatsCollector::~MessageCenterStatsCollector() {
53 message_center_
->RemoveObserver(this);
56 void MessageCenterStatsCollector::OnNotificationAdded(
57 const std::string
& notification_id
) {
58 stats_
[notification_id
] = NotificationStats(notification_id
);
60 StatsCollection::iterator iter
= stats_
.find(notification_id
);
61 DCHECK(iter
!= stats_
.end());
63 stats_
[notification_id
].CollectAction(NOTIFICATION_ACTION_ADD
);
66 void MessageCenterStatsCollector::OnNotificationRemoved(
67 const std::string
& notification_id
, bool by_user
) {
68 StatsCollection::iterator iter
= stats_
.find(notification_id
);
69 if (iter
== stats_
.end())
71 NotificationStats
& notification_stat
= iter
->second
;
72 notification_stat
.CollectAction(by_user
?
73 NOTIFICATION_ACTION_CLOSE_BY_USER
:
74 NOTIFICATION_ACTION_CLOSE_BY_SYSTEM
);
75 notification_stat
.RecordAggregateStats();
76 stats_
.erase(notification_id
);
79 void MessageCenterStatsCollector::OnNotificationUpdated(
80 const std::string
& notification_id
) {
81 StatsCollection::iterator iter
= stats_
.find(notification_id
);
82 if (iter
== stats_
.end())
84 NotificationStats
& notification_stat
= iter
->second
;
86 notification_stat
.CollectAction(NOTIFICATION_ACTION_UPDATE
);
89 void MessageCenterStatsCollector::OnNotificationClicked(
90 const std::string
& notification_id
) {
91 StatsCollection::iterator iter
= stats_
.find(notification_id
);
92 if (iter
== stats_
.end())
94 NotificationStats
& notification_stat
= iter
->second
;
96 notification_stat
.CollectAction(NOTIFICATION_ACTION_CLICK
);
99 void MessageCenterStatsCollector::OnNotificationButtonClicked(
100 const std::string
& notification_id
,
102 StatsCollection::iterator iter
= stats_
.find(notification_id
);
103 if (iter
== stats_
.end())
105 NotificationStats
& notification_stat
= iter
->second
;
107 notification_stat
.CollectAction(NOTIFICATION_ACTION_BUTTON_CLICK
);
110 void MessageCenterStatsCollector::OnNotificationDisplayed(
111 const std::string
& notification_id
,
112 const message_center::DisplaySource source
) {
113 StatsCollection::iterator iter
= stats_
.find(notification_id
);
114 if (iter
== stats_
.end())
116 NotificationStats
& notification_stat
= iter
->second
;
118 notification_stat
.CollectAction(NOTIFICATION_ACTION_DISPLAY
);
121 void MessageCenterStatsCollector::OnCenterVisibilityChanged(
122 message_center::Visibility visibility
) {
123 switch (visibility
) {
124 case message_center::VISIBILITY_TRANSIENT
:
126 case message_center::VISIBILITY_MESSAGE_CENTER
:
127 content::RecordAction(
128 base::UserMetricsAction("Notifications.ShowMessageCenter"));
130 case message_center::VISIBILITY_SETTINGS
:
131 content::RecordAction(
132 base::UserMetricsAction("Notifications.ShowSettings"));
137 void MessageCenterStatsCollector::OnQuietModeChanged(bool in_quiet_mode
) {
139 content::RecordAction(
140 base::UserMetricsAction("Notifications.Mute"));
142 content::RecordAction(
143 base::UserMetricsAction("Notifications.Unmute"));