metrics_utils.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // Copyright (c) 2020 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.
  4. #include "ash/system/message_center/metrics_utils.h"
  5. #include "ash/constants/notifier_catalogs.h"
  6. #include "ash/public/cpp/notification_utils.h"
  7. #include "base/metrics/histogram_functions.h"
  8. #include "base/metrics/histogram_macros.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "ui/message_center/message_center.h"
  12. #include "ui/message_center/public/cpp/notifier_id.h"
  13. #include "ui/message_center/views/message_view.h"
  14. namespace {
  15. // Used in histogram names that are persisted to metric logs.
  16. std::string GetNotifierFrameworkNotificationHistogramBase(bool pinned) {
  17. if (pinned)
  18. return "Ash.NotifierFramework.PinnedSystemNotification";
  19. return "Ash.NotifierFramework.SystemNotification";
  20. }
  21. // Used in histogram names that are persisted to metric logs.
  22. std::string GetNotifierFrameworkPopupDismissedTimeRange(
  23. const base::TimeDelta& time) {
  24. if (time <= base::Seconds(1))
  25. return "Within1s";
  26. if (time <= base::Seconds(3))
  27. return "Within3s";
  28. if (time <= base::Seconds(7))
  29. return "Within7s";
  30. return "After7s";
  31. }
  32. bool ValidCatalogName(const message_center::NotifierId& notifier_id) {
  33. if (notifier_id.type != message_center::NotifierType::SYSTEM_COMPONENT)
  34. return false;
  35. if (notifier_id.catalog_name == ash::NotificationCatalogName::kNone ||
  36. notifier_id.catalog_name ==
  37. ash::NotificationCatalogName::kTestCatalogName) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. } // namespace
  43. namespace ash {
  44. namespace metrics_utils {
  45. NotificationTypeDetailed GetNotificationTypeForChromeApp(
  46. const message_center::Notification& notification) {
  47. int priority = notification.rich_notification_data().priority;
  48. bool require_interaction =
  49. notification.rich_notification_data().never_timeout;
  50. if (require_interaction) {
  51. switch (priority) {
  52. case -2:
  53. return CHROME_APP_PRIORITY_MINUS_TWO_REQUIRE_INTERACTION;
  54. case -1:
  55. return CHROME_APP_PRIORITY_MINUS_ONE_REQUIRE_INTERACTION;
  56. case 0:
  57. return CHROME_APP_PRIORITY_ZERO_REQUIRE_INTERACTION;
  58. case 1:
  59. return CHROME_APP_PRIORITY_ONE_REQUIRE_INTERACTION;
  60. case 2:
  61. return CHROME_APP_PRIORITY_TWO_REQUIRE_INTERACTION;
  62. default:
  63. NOTREACHED();
  64. return OTHER;
  65. }
  66. } else {
  67. switch (priority) {
  68. case -2:
  69. return CHROME_APP_PRIORITY_MINUS_TWO;
  70. case -1:
  71. return CHROME_APP_PRIORITY_MINUS_ONE;
  72. case 0:
  73. return CHROME_APP_PRIORITY_ZERO;
  74. case 1:
  75. return CHROME_APP_PRIORITY_ONE;
  76. case 2:
  77. return CHROME_APP_PRIORITY_TWO;
  78. default:
  79. NOTREACHED();
  80. return OTHER;
  81. }
  82. }
  83. }
  84. NotificationTypeDetailed GetNotificationTypeForArc(
  85. const message_center::Notification& notification) {
  86. int priority = notification.rich_notification_data().priority;
  87. bool pinned = notification.rich_notification_data().pinned;
  88. if (pinned) {
  89. switch (priority) {
  90. case -2:
  91. return ARC_PRIORITY_MINUS_TWO_PINNED;
  92. case -1:
  93. return ARC_PRIORITY_MINUS_ONE_PINNED;
  94. case 0:
  95. return ARC_PRIORITY_ZERO_PINNED;
  96. case 1:
  97. return ARC_PRIORITY_ONE_PINNED;
  98. case 2:
  99. return ARC_PRIORITY_TWO_PINNED;
  100. default:
  101. NOTREACHED();
  102. return OTHER;
  103. }
  104. } else {
  105. switch (priority) {
  106. case -2:
  107. return ARC_PRIORITY_MINUS_TWO;
  108. case -1:
  109. return ARC_PRIORITY_MINUS_ONE;
  110. case 0:
  111. return ARC_PRIORITY_ZERO;
  112. case 1:
  113. return ARC_PRIORITY_ONE;
  114. case 2:
  115. return ARC_PRIORITY_TWO;
  116. default:
  117. NOTREACHED();
  118. return OTHER;
  119. }
  120. }
  121. }
  122. NotificationTypeDetailed GetNotificationTypeForCrosSystemPriority(
  123. const message_center::Notification& notification) {
  124. // The warning level is not stored in the notification data, so we need to
  125. // infer it from the accent color.
  126. absl::optional<SkColor> accent_color =
  127. notification.rich_notification_data().accent_color;
  128. message_center::SystemNotificationWarningLevel warning_level =
  129. message_center::SystemNotificationWarningLevel::NORMAL;
  130. if (accent_color.has_value()) {
  131. if (accent_color.value() == kSystemNotificationColorWarning) {
  132. warning_level = message_center::SystemNotificationWarningLevel::WARNING;
  133. } else if (accent_color.value() ==
  134. kSystemNotificationColorCriticalWarning) {
  135. warning_level =
  136. message_center::SystemNotificationWarningLevel::CRITICAL_WARNING;
  137. }
  138. }
  139. bool pinned = notification.rich_notification_data().pinned;
  140. if (pinned) {
  141. switch (warning_level) {
  142. case message_center::SystemNotificationWarningLevel::NORMAL:
  143. return CROS_SYSTEM_PRIORITY_PINNED;
  144. case message_center::SystemNotificationWarningLevel::WARNING:
  145. return CROS_SYSTEM_PRIORITY_WARNING_PINNED;
  146. case message_center::SystemNotificationWarningLevel::CRITICAL_WARNING:
  147. return CROS_SYSTEM_PRIORITY_CRITICAL_WARNING_PINNED;
  148. }
  149. } else {
  150. switch (warning_level) {
  151. case message_center::SystemNotificationWarningLevel::NORMAL:
  152. return CROS_SYSTEM_PRIORITY;
  153. case message_center::SystemNotificationWarningLevel::WARNING:
  154. return CROS_SYSTEM_PRIORITY_WARNING;
  155. case message_center::SystemNotificationWarningLevel::CRITICAL_WARNING:
  156. return CROS_SYSTEM_PRIORITY_CRITICAL_WARNING;
  157. }
  158. }
  159. }
  160. NotificationTypeDetailed GetNotificationTypeForCros(
  161. const message_center::Notification& notification) {
  162. int priority = notification.rich_notification_data().priority;
  163. if (priority == message_center::SYSTEM_PRIORITY)
  164. return GetNotificationTypeForCrosSystemPriority(notification);
  165. bool pinned = notification.rich_notification_data().pinned;
  166. if (pinned) {
  167. switch (priority) {
  168. case -2:
  169. return CROS_PRIORITY_MINUS_TWO_PINNED;
  170. case -1:
  171. return CROS_PRIORITY_MINUS_ONE_PINNED;
  172. case 0:
  173. return CROS_PRIORITY_ZERO_PINNED;
  174. case 1:
  175. return CROS_PRIORITY_ONE_PINNED;
  176. case 2:
  177. return CROS_PRIORITY_TWO_PINNED;
  178. default:
  179. NOTREACHED();
  180. return OTHER;
  181. }
  182. } else {
  183. switch (priority) {
  184. case -2:
  185. return CROS_PRIORITY_MINUS_TWO;
  186. case -1:
  187. return CROS_PRIORITY_MINUS_ONE;
  188. case 0:
  189. return CROS_PRIORITY_ZERO;
  190. case 1:
  191. return CROS_PRIORITY_ONE;
  192. case 2:
  193. return CROS_PRIORITY_TWO;
  194. default:
  195. NOTREACHED();
  196. return OTHER;
  197. }
  198. }
  199. }
  200. NotificationTypeDetailed GetNotificationTypeForWeb(
  201. const message_center::Notification& notification) {
  202. bool require_interaction =
  203. notification.rich_notification_data().never_timeout;
  204. return require_interaction ? WEB_REQUIRE_INTERACTION : WEB;
  205. }
  206. NotificationTypeDetailed GetNotificationTypeForPhoneHub(
  207. const message_center::Notification& notification) {
  208. int priority = notification.rich_notification_data().priority;
  209. switch (priority) {
  210. case -2:
  211. return PHONEHUB_PRIORITY_MINUS_TWO;
  212. case -1:
  213. return PHONEHUB_PRIORITY_MINUS_ONE;
  214. case 0:
  215. return PHONEHUB_PRIORITY_ZERO;
  216. case 1:
  217. return PHONEHUB_PRIORITY_ONE;
  218. case 2:
  219. return PHONEHUB_PRIORITY_TWO;
  220. default:
  221. NOTREACHED();
  222. return OTHER;
  223. }
  224. }
  225. NotificationTypeDetailed GetNotificationType(
  226. const message_center::Notification& notification) {
  227. message_center::NotifierType notifier_type = notification.notifier_id().type;
  228. switch (notifier_type) {
  229. case message_center::NotifierType::APPLICATION:
  230. return GetNotificationTypeForChromeApp(notification);
  231. case message_center::NotifierType::ARC_APPLICATION:
  232. return GetNotificationTypeForArc(notification);
  233. case message_center::NotifierType::WEB_PAGE:
  234. return GetNotificationTypeForWeb(notification);
  235. case message_center::NotifierType::SYSTEM_COMPONENT:
  236. return GetNotificationTypeForCros(notification);
  237. case message_center::NotifierType::PHONE_HUB:
  238. return GetNotificationTypeForPhoneHub(notification);
  239. case message_center::NotifierType::CROSTINI_APPLICATION:
  240. default:
  241. return OTHER;
  242. }
  243. }
  244. absl::optional<NotificationViewType> GetNotificationViewType(
  245. message_center::Notification* notification) {
  246. absl::optional<NotificationViewType> type;
  247. // Ignore ARC notification since its view is rendered through Android, and
  248. // its notification metadata for image and buttons is empty. Also ignore group
  249. // parent notification since it only serves as a placeholder.
  250. if (!notification ||
  251. notification->notifier_id().type ==
  252. message_center::NotifierType::ARC_APPLICATION ||
  253. notification->group_parent())
  254. return type;
  255. bool has_inline_reply = false;
  256. for (auto button_info : notification->buttons()) {
  257. if (button_info.placeholder) {
  258. has_inline_reply = true;
  259. break;
  260. }
  261. }
  262. bool has_buttons = !notification->buttons().empty();
  263. bool has_image = !notification->image().IsEmpty();
  264. bool is_grouped_child = notification->group_child();
  265. if (has_inline_reply) {
  266. if (has_image) {
  267. type = is_grouped_child
  268. ? NotificationViewType::GROUPED_HAS_IMAGE_AND_INLINE_REPLY
  269. : NotificationViewType::HAS_IMAGE_AND_INLINE_REPLY;
  270. } else {
  271. type = is_grouped_child ? NotificationViewType::GROUPED_HAS_INLINE_REPLY
  272. : NotificationViewType::HAS_INLINE_REPLY;
  273. }
  274. return type;
  275. }
  276. if (has_buttons) {
  277. if (has_image) {
  278. type = is_grouped_child
  279. ? NotificationViewType::GROUPED_HAS_IMAGE_AND_ACTION
  280. : NotificationViewType::HAS_IMAGE_AND_ACTION;
  281. } else {
  282. type = is_grouped_child ? NotificationViewType::GROUPED_HAS_ACTION
  283. : NotificationViewType::HAS_ACTION;
  284. }
  285. return type;
  286. }
  287. if (has_image) {
  288. return is_grouped_child ? NotificationViewType::GROUPED_HAS_IMAGE
  289. : NotificationViewType::HAS_IMAGE;
  290. } else {
  291. return is_grouped_child ? NotificationViewType::GROUPED_SIMPLE
  292. : NotificationViewType::SIMPLE;
  293. }
  294. }
  295. absl::optional<NotificationTypeDetailed> GetNotificationType(
  296. const std::string& notification_id) {
  297. absl::optional<NotificationTypeDetailed> type;
  298. auto* notification =
  299. message_center::MessageCenter::Get()->FindVisibleNotificationById(
  300. notification_id);
  301. if (!notification)
  302. return type;
  303. type = GetNotificationType(*notification);
  304. return type;
  305. }
  306. void LogHover(const std::string& notification_id, bool is_popup) {
  307. auto type = GetNotificationType(notification_id);
  308. if (!type.has_value())
  309. return;
  310. if (is_popup) {
  311. base::UmaHistogramEnumeration("Notifications.Cros.Actions.Popup.Hover",
  312. type.value());
  313. } else {
  314. base::UmaHistogramEnumeration("Notifications.Cros.Actions.Tray.Hover",
  315. type.value());
  316. }
  317. }
  318. void LogClickedBody(const std::string& notification_id, bool is_popup) {
  319. auto* notification =
  320. message_center::MessageCenter::Get()->FindVisibleNotificationById(
  321. notification_id);
  322. if (!notification)
  323. return;
  324. auto type = GetNotificationType(*notification);
  325. if (is_popup) {
  326. UMA_HISTOGRAM_ENUMERATION("Notifications.Cros.Actions.Popup.ClickedBody",
  327. type);
  328. } else {
  329. UMA_HISTOGRAM_ENUMERATION("Notifications.Cros.Actions.Tray.ClickedBody",
  330. type);
  331. }
  332. // If notification's delegate is null, that means the notification is not
  333. // clickable and the user just did a "bad click", which is a click that did
  334. // not do anything.
  335. if (notification->delegate()) {
  336. base::UmaHistogramEnumeration(
  337. "Notifications.Cros.Actions.ClickedBody.GoodClick", type);
  338. } else {
  339. base::UmaHistogramEnumeration(
  340. "Notifications.Cros.Actions.ClickedBody.BadClick", type);
  341. }
  342. }
  343. void LogClickedActionButton(const std::string& notification_id, bool is_popup) {
  344. auto type = GetNotificationType(notification_id);
  345. if (!type.has_value())
  346. return;
  347. if (is_popup) {
  348. UMA_HISTOGRAM_ENUMERATION(
  349. "Notifications.Cros.Actions.Popup.ClickedActionButton", type.value());
  350. } else {
  351. UMA_HISTOGRAM_ENUMERATION(
  352. "Notifications.Cros.Actions.Tray.ClickedActionButton", type.value());
  353. }
  354. }
  355. void LogInlineReplySent(const std::string& notification_id, bool is_popup) {
  356. auto type = GetNotificationType(notification_id);
  357. if (!type.has_value())
  358. return;
  359. if (is_popup) {
  360. base::UmaHistogramEnumeration(
  361. "Notifications.Cros.Actions.Popup.InlineReplySent", type.value());
  362. } else {
  363. base::UmaHistogramEnumeration(
  364. "Notifications.Cros.Actions.Tray.InlineReplySent", type.value());
  365. }
  366. }
  367. void LogPopupExpiredToTray(const std::string& notification_id) {
  368. auto type = GetNotificationType(notification_id);
  369. if (!type.has_value())
  370. return;
  371. UMA_HISTOGRAM_ENUMERATION("Notifications.Cros.Actions.Popup.ExpireToTray",
  372. type.value());
  373. }
  374. void LogClosedByUser(const std::string& notification_id,
  375. bool is_swipe,
  376. bool is_popup) {
  377. auto type = GetNotificationType(notification_id);
  378. if (!type.has_value())
  379. return;
  380. if (is_popup) {
  381. if (is_swipe) {
  382. UMA_HISTOGRAM_ENUMERATION(
  383. "Notifications.Cros.Actions.Popup.ClosedByUser.Swipe", type.value());
  384. } else {
  385. UMA_HISTOGRAM_ENUMERATION(
  386. "Notifications.Cros.Actions.Popup.ClosedByUser.Click", type.value());
  387. }
  388. } else {
  389. if (is_swipe) {
  390. UMA_HISTOGRAM_ENUMERATION(
  391. "Notifications.Cros.Actions.Tray.ClosedByUser.Swipe", type.value());
  392. } else {
  393. UMA_HISTOGRAM_ENUMERATION(
  394. "Notifications.Cros.Actions.Tray.ClosedByUser.Click", type.value());
  395. }
  396. }
  397. }
  398. void LogSettingsShown(const std::string& notification_id,
  399. bool is_slide_controls,
  400. bool is_popup) {
  401. auto type = GetNotificationType(notification_id);
  402. if (!type.has_value())
  403. return;
  404. if (is_popup) {
  405. DCHECK(!is_slide_controls);
  406. UMA_HISTOGRAM_ENUMERATION(
  407. "Notifications.Cros.Actions.Popup.SettingsShown.HoverControls",
  408. type.value());
  409. } else {
  410. if (is_slide_controls) {
  411. UMA_HISTOGRAM_ENUMERATION(
  412. "Notifications.Cros.Actions.Tray.SettingsShown.SlideControls",
  413. type.value());
  414. } else {
  415. UMA_HISTOGRAM_ENUMERATION(
  416. "Notifications.Cros.Actions.Tray.SettingsShown.HoverControls",
  417. type.value());
  418. }
  419. }
  420. }
  421. void LogSnoozed(const std::string& notification_id,
  422. bool is_slide_controls,
  423. bool is_popup) {
  424. auto type = GetNotificationType(notification_id);
  425. if (!type.has_value())
  426. return;
  427. if (is_popup) {
  428. DCHECK(!is_slide_controls);
  429. UMA_HISTOGRAM_ENUMERATION(
  430. "Notifications.Cros.Actions.Popup.Snoozed.HoverControls", type.value());
  431. } else {
  432. if (is_slide_controls) {
  433. UMA_HISTOGRAM_ENUMERATION(
  434. "Notifications.Cros.Actions.Tray.Snoozed.SlideControls",
  435. type.value());
  436. } else {
  437. UMA_HISTOGRAM_ENUMERATION(
  438. "Notifications.Cros.Actions.Tray.Snoozed.HoverControls",
  439. type.value());
  440. }
  441. }
  442. }
  443. void LogPopupShown(const std::string& notification_id) {
  444. auto type = GetNotificationType(notification_id);
  445. if (!type.has_value())
  446. return;
  447. UMA_HISTOGRAM_ENUMERATION("Notifications.Cros.Actions.Popup.Shown",
  448. type.value());
  449. auto* notification =
  450. message_center::MessageCenter::Get()->FindNotificationById(
  451. notification_id);
  452. if (!notification)
  453. return;
  454. if (!ValidCatalogName(notification->notifier_id()))
  455. return;
  456. const std::string histogram_base =
  457. GetNotifierFrameworkNotificationHistogramBase(notification->pinned());
  458. base::UmaHistogramEnumeration(
  459. base::StringPrintf("%s.Popup.ShownCount", histogram_base.c_str()),
  460. notification->notifier_id().catalog_name);
  461. }
  462. void LogPopupClosed(message_center::MessagePopupView* popup) {
  463. const message_center::NotifierId notifier_id =
  464. popup->message_view()->notifier_id();
  465. if (!ValidCatalogName(notifier_id))
  466. return;
  467. const std::string histogram_base =
  468. GetNotifierFrameworkNotificationHistogramBase(
  469. popup->message_view()->pinned());
  470. const base::TimeDelta user_journey_time =
  471. base::Time::Now() - popup->message_view()->timestamp();
  472. const std::string time_range =
  473. GetNotifierFrameworkPopupDismissedTimeRange(user_journey_time);
  474. base::UmaHistogramMediumTimes(
  475. base::StringPrintf("%s.Popup.UserJourneyTime", histogram_base.c_str()),
  476. user_journey_time);
  477. base::UmaHistogramEnumeration(
  478. base::StringPrintf("%s.Popup.Dismissed.%s", histogram_base.c_str(),
  479. time_range.c_str()),
  480. notifier_id.catalog_name);
  481. }
  482. void LogClosedByClearAll(const std::string& notification_id) {
  483. auto type = GetNotificationType(notification_id);
  484. if (!type.has_value())
  485. return;
  486. UMA_HISTOGRAM_ENUMERATION("Notifications.Cros.Actions.Tray.ClosedByClearAll",
  487. type.value());
  488. }
  489. void LogNotificationAdded(const std::string& notification_id) {
  490. LogSystemNotificationAdded(notification_id);
  491. auto* notification =
  492. message_center::MessageCenter::Get()->FindVisibleNotificationById(
  493. notification_id);
  494. if (!notification)
  495. return;
  496. base::UmaHistogramEnumeration("Notifications.Cros.Actions.NotificationAdded",
  497. GetNotificationType(*notification));
  498. auto notification_view_type = GetNotificationViewType(notification);
  499. if (!notification_view_type)
  500. return;
  501. base::UmaHistogramEnumeration("Ash.NotificationView.NotificationAdded.Type",
  502. notification_view_type.value());
  503. }
  504. void LogSystemNotificationAdded(const std::string& notification_id) {
  505. auto* notification =
  506. message_center::MessageCenter::Get()->FindNotificationById(
  507. notification_id);
  508. if (!notification)
  509. return;
  510. if (!ValidCatalogName(notification->notifier_id()))
  511. return;
  512. const std::string histogram_base =
  513. GetNotifierFrameworkNotificationHistogramBase(notification->pinned());
  514. base::UmaHistogramEnumeration(
  515. base::StringPrintf("%s.Added", histogram_base.c_str()),
  516. notification->notifier_id().catalog_name);
  517. }
  518. void LogNotificationsShownInFirstMinute(int count) {
  519. UMA_HISTOGRAM_COUNTS_1000(
  520. "Notifications.Cros.Actions.CountOfNotificationShownInFirstMinutePerUser",
  521. count);
  522. }
  523. void LogCountOfNotificationsInOneGroup(int notification_count) {
  524. // `notification_count` might be zero if the parent doesn't have any child
  525. // notification left after remove.
  526. if (notification_count <= 0)
  527. return;
  528. base::UmaHistogramCounts100("Ash.Notification.CountOfNotificationsInOneGroup",
  529. notification_count);
  530. }
  531. void LogExpandButtonClickAction(ExpandButtonClickAction action) {
  532. base::UmaHistogramEnumeration("Ash.NotificationView.ExpandButton.ClickAction",
  533. action);
  534. }
  535. void LogGroupNotificationAddedType(GroupNotificationType type) {
  536. base::UmaHistogramEnumeration("Ash.Notification.GroupNotificationAdded",
  537. type);
  538. }
  539. } // namespace metrics_utils
  540. } // namespace ash