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   void OnSuggestionOfflinePageVisited(base::TimeDelta visit_time,
78                                       bool return_to_ntp);
79 
80   // Should only be called once per NTP for each "more" button.
81   void OnMoreButtonShown(int position);
82 
83   void OnMoreButtonClicked(int position);
84 
85   void OnNotInterestedInSource(int position, bool committed);
86 
87   void OnNotInterestedInTopic(int position, bool committed);
88 
89   void OnSpinnerStarted(int spinner_type);
90 
91   void OnSpinnerFinished(base::TimeDelta shown_time, int spinner_type);
92 
93   void OnSpinnerDestroyedWithoutCompleting(base::TimeDelta shown_time,
94                                            int spinner_type);
95 
96   void OnPietFrameRenderingEvent(std::vector<int> piet_error_codes);
97 
98   void OnVisualElementClicked(int element_type,
99                               int position,
100                               base::Time fetch_date);
101 
102   void OnVisualElementViewed(int element_type,
103                              int position,
104                              base::Time fetch_date);
105 
106   void OnInternalError(int internal_error);
107 
108   void OnTokenCompleted(bool was_synthetic, int content_count, int token_count);
109 
110   void OnTokenFailedToComplete(bool was_synthetic, int failure_count);
111 
112   void OnServerRequest(int request_reason);
113 
114   void OnZeroStateShown(int zero_state_show_reason);
115 
116   void OnZeroStateRefreshCompleted(int new_content_count, int new_token_count);
117 
118   void OnTaskFinished(int task_type, int delay_time_ms, int task_time_ms);
119 
120   void ReportScrolledAfterOpen();
121 
122  private:
123   const HistoryURLCheckCallback history_url_check_callback_;
124 
125   // Used to access current time, injected for testing.
126   base::Clock* clock_;
127 
128   FeedSchedulerHost* scheduler_host_;
129 
130   base::WeakPtrFactory<FeedLoggingMetrics> weak_ptr_factory_{this};
131 
132   DISALLOW_COPY_AND_ASSIGN(FeedLoggingMetrics);
133 };
134 
135 }  // namespace feed
136 
137 #endif  // COMPONENTS_FEED_CORE_FEED_LOGGING_METRICS_H_
138