1 // Copyright 2018 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 
5 #ifndef COMPONENTS_FEED_CORE_FEED_LOGGING_METRICS_H_
6 #define COMPONENTS_FEED_CORE_FEED_LOGGING_METRICS_H_
7 
8 #include <memory>
9 #include <utility>
10 #include <vector>
11 
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "components/feed/core/feed_scheduler_host.h"
16 #include "url/gurl.h"
17 
18 namespace base {
19 class Clock;
20 class Time;
21 class TimeDelta;
22 }  // namespace base
23 
24 enum class WindowOpenDisposition;
25 
26 namespace feed {
27 
28 // FeedLoggingMetrics is a central place to report all the UMA metrics for Feed.
29 class FeedLoggingMetrics {
30  public:
31   // Return whether the URL is visited when calling checking URL visited.
32   using CheckURLVisitCallback = base::OnceCallback<void(bool)>;
33 
34   // Calling this callback when need to check whether url is visited, and will
35   // tell CheckURLVisitCallback the result.
36   using HistoryURLCheckCallback =
37       base::RepeatingCallback<void(const GURL&, CheckURLVisitCallback)>;
38 
39   explicit FeedLoggingMetrics(HistoryURLCheckCallback callback,
40                               base::Clock* clock,
41                               FeedSchedulerHost* scheduler_host);
42   ~FeedLoggingMetrics();
43 
44   // |suggestions_count| contains how many cards show to users. It does not
45   // depend on whether the user actually saw the cards.
46   void OnPageShown(const int suggestions_count);
47 
48   // The amount of time for the Feed to populate articles. This does not include
49   // time to render but time to populate data in the UI.
50   void OnPagePopulated(base::TimeDelta timeToPopulate);
51 
52   // Should only be called once per NTP for each suggestion.
53   void OnSuggestionShown(int position,
54                          base::Time publish_date,
55                          float score,
56                          base::Time fetch_date,
57                          bool is_available_offline);
58 
59   void OnSuggestionOpened(int position,
60                           base::Time publish_date,
61                           float score,
62                           bool is_available_offline);
63 
64   void OnSuggestionWindowOpened(WindowOpenDisposition disposition);
65 
66   void OnSuggestionMenuOpened(int position,
67                               base::Time publish_date,
68                               float score);
69 
70   void OnSuggestionDismissed(int position, const GURL& url, bool committed);
71 
72   void OnSuggestionSwiped();
73 
74   void OnSuggestionArticleVisited(base::TimeDelta visit_time,
75                                   bool return_to_ntp);
76 
77   // Should only be called once per NTP for each "more" button.
78   void OnMoreButtonShown(int position);
79 
80   void OnMoreButtonClicked(int position);
81 
82   void OnNotInterestedInSource(int position, bool committed);
83 
84   void OnNotInterestedInTopic(int position, bool committed);
85 
86   void OnSpinnerStarted(int spinner_type);
87 
88   void OnSpinnerFinished(base::TimeDelta shown_time, int spinner_type);
89 
90   void OnSpinnerDestroyedWithoutCompleting(base::TimeDelta shown_time,
91                                            int spinner_type);
92 
93   void OnPietFrameRenderingEvent(std::vector<int> piet_error_codes);
94 
95   void OnVisualElementClicked(int element_type,
96                               int position,
97                               base::Time fetch_date);
98 
99   void OnVisualElementViewed(int element_type,
100                              int position,
101                              base::Time fetch_date);
102 
103   void OnInternalError(int internal_error);
104 
105   void OnTokenCompleted(bool was_synthetic, int content_count, int token_count);
106 
107   void OnTokenFailedToComplete(bool was_synthetic, int failure_count);
108 
109   void OnServerRequest(int request_reason);
110 
111   void OnZeroStateShown(int zero_state_show_reason);
112 
113   void OnZeroStateRefreshCompleted(int new_content_count, int new_token_count);
114 
115   void OnTaskFinished(int task_type, int delay_time_ms, int task_time_ms);
116 
117   void ReportScrolledAfterOpen();
118 
119  private:
120   const HistoryURLCheckCallback history_url_check_callback_;
121 
122   // Used to access current time, injected for testing.
123   base::Clock* clock_;
124 
125   FeedSchedulerHost* scheduler_host_;
126 
127   base::WeakPtrFactory<FeedLoggingMetrics> weak_ptr_factory_{this};
128 
129   DISALLOW_COPY_AND_ASSIGN(FeedLoggingMetrics);
130 };
131 
132 }  // namespace feed
133 
134 #endif  // COMPONENTS_FEED_CORE_FEED_LOGGING_METRICS_H_
135