OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" | 5 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/search/most_visited_iframe_source.h" | 10 #include "chrome/browser/search/most_visited_iframe_source.h" |
11 #include "chrome/browser/search/search.h" | 11 #include "chrome/browser/search/search.h" |
12 #include "chrome/common/search_urls.h" | 12 #include "chrome/common/search_urls.h" |
13 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
14 #include "content/public/browser/navigation_details.h" | 14 #include "content/public/browser/navigation_details.h" |
15 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
| 16 #include "content/public/browser/user_metrics.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 | 18 |
18 // Macro to log UMA statistics related to the 8 tiles shown on the NTP. | 19 // Macro to log UMA statistics related to the 8 tiles shown on the NTP. |
19 #define UMA_HISTOGRAM_NTP_TILES(name, sample) \ | 20 #define UMA_HISTOGRAM_NTP_TILES(name, sample) \ |
20 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 0, 8, 9) | 21 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 0, 8, 9) |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Used to track if suggestions were issued by the client or the server. | 25 // Used to track if suggestions were issued by the client or the server. |
25 enum SuggestionsType { | 26 enum SuggestionsType { |
26 CLIENT_SIDE = 0, | 27 CLIENT_SIDE = 0, |
27 SERVER_SIDE = 1, | 28 SERVER_SIDE = 1, |
28 SUGGESTIONS_TYPE_COUNT = 2 | 29 SUGGESTIONS_TYPE_COUNT = 2 |
29 }; | 30 }; |
30 | 31 |
| 32 // Number of Most Visited elements on the NTP for logging purposes. |
| 33 const int kNumMostVisited = 8; |
| 34 |
| 35 // Name of the histogram keeping track of Most Visited impressions. |
| 36 const char kMostVisitedImpressionHistogramName[] = |
| 37 "NewTabPage.SuggestionsImpression"; |
| 38 |
31 // Format string to generate the name for the histogram keeping track of | 39 // Format string to generate the name for the histogram keeping track of |
32 // suggestion impressions. | 40 // suggestion impressions. |
33 const char kImpressionHistogramWithProvider[] = | 41 const char kMostVisitedImpressionHistogramWithProvider[] = |
34 "NewTabPage.SuggestionsImpression.%s"; | 42 "NewTabPage.SuggestionsImpression.%s"; |
35 | 43 |
| 44 // Name of the histogram keeping track of Most Visited navigations. |
| 45 const char kMostVisitedNavigationHistogramName[] = |
| 46 "NewTabPage.MostVisited"; |
| 47 |
| 48 // Format string to generate the name for the histogram keeping track of |
| 49 // suggestion navigations. |
| 50 const char kMostVisitedNavigationHistogramWithProvider[] = |
| 51 "NewTabPage.MostVisited.%s"; |
| 52 |
36 } // namespace | 53 } // namespace |
37 | 54 |
38 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NTPUserDataLogger); | 55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NTPUserDataLogger); |
39 | 56 |
40 NTPUserDataLogger::~NTPUserDataLogger() {} | 57 NTPUserDataLogger::~NTPUserDataLogger() {} |
41 | 58 |
42 // static | 59 // static |
43 NTPUserDataLogger* NTPUserDataLogger::GetOrCreateFromWebContents( | 60 NTPUserDataLogger* NTPUserDataLogger::GetOrCreateFromWebContents( |
44 content::WebContents* content) { | 61 content::WebContents* content) { |
45 // Calling CreateForWebContents when an instance is already attached has no | 62 // Calling CreateForWebContents when an instance is already attached has no |
46 // effect, so we can do this. | 63 // effect, so we can do this. |
47 NTPUserDataLogger::CreateForWebContents(content); | 64 NTPUserDataLogger::CreateForWebContents(content); |
48 NTPUserDataLogger* logger = NTPUserDataLogger::FromWebContents(content); | 65 NTPUserDataLogger* logger = NTPUserDataLogger::FromWebContents(content); |
49 | 66 |
50 // We record the URL of this NTP in order to identify navigations that | 67 // We record the URL of this NTP in order to identify navigations that |
51 // originate from it. We use the NavigationController's URL since it might | 68 // originate from it. We use the NavigationController's URL since it might |
52 // differ from the WebContents URL which is usually chrome://newtab/. | 69 // differ from the WebContents URL which is usually chrome://newtab/. |
53 const content::NavigationEntry* entry = | 70 const content::NavigationEntry* entry = |
54 content->GetController().GetVisibleEntry(); | 71 content->GetController().GetVisibleEntry(); |
55 if (entry) | 72 if (entry) |
56 logger->ntp_url_ = entry->GetURL(); | 73 logger->ntp_url_ = entry->GetURL(); |
57 | 74 |
58 return logger; | 75 return logger; |
59 } | 76 } |
60 | 77 |
| 78 // static |
| 79 std::string NTPUserDataLogger::GetMostVisitedImpressionHistogramNameForProvider( |
| 80 const std::string& provider) { |
| 81 return base::StringPrintf(kMostVisitedImpressionHistogramWithProvider, |
| 82 provider.c_str()); |
| 83 } |
| 84 |
| 85 // static |
| 86 std::string NTPUserDataLogger::GetMostVisitedNavigationHistogramNameForProvider( |
| 87 const std::string& provider) { |
| 88 return base::StringPrintf(kMostVisitedNavigationHistogramWithProvider, |
| 89 provider.c_str()); |
| 90 } |
| 91 |
61 void NTPUserDataLogger::EmitNtpStatistics() { | 92 void NTPUserDataLogger::EmitNtpStatistics() { |
62 UMA_HISTOGRAM_COUNTS("NewTabPage.NumberOfMouseOvers", number_of_mouseovers_); | 93 UMA_HISTOGRAM_COUNTS("NewTabPage.NumberOfMouseOvers", number_of_mouseovers_); |
63 number_of_mouseovers_ = 0; | 94 number_of_mouseovers_ = 0; |
64 | 95 |
65 // Only log the following statistics if at least one tile is recorded. This | 96 // Only log the following statistics if at least one tile is recorded. This |
66 // check is required because the statistics are emitted whenever the user | 97 // check is required because the statistics are emitted whenever the user |
67 // changes tab away from the NTP. However, if the user comes back to that NTP | 98 // changes tab away from the NTP. However, if the user comes back to that NTP |
68 // later the statistics are not regenerated (i.e. they are all 0). If we log | 99 // later the statistics are not regenerated (i.e. they are all 0). If we log |
69 // them again we get a strong bias. | 100 // them again we get a strong bias. |
70 if (number_of_tiles_ > 0) { | 101 if (number_of_tiles_ > 0) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 number_of_external_tile_fallbacks_++; | 160 number_of_external_tile_fallbacks_++; |
130 break; | 161 break; |
131 case NTP_MOUSEOVER: | 162 case NTP_MOUSEOVER: |
132 number_of_mouseovers_++; | 163 number_of_mouseovers_++; |
133 break; | 164 break; |
134 default: | 165 default: |
135 NOTREACHED(); | 166 NOTREACHED(); |
136 } | 167 } |
137 } | 168 } |
138 | 169 |
139 void NTPUserDataLogger::LogImpression(int position, | 170 void NTPUserDataLogger::LogMostVisitedImpression( |
140 const base::string16& provider) { | 171 int position, const base::string16& provider) { |
141 // Cannot rely on UMA histograms macro because the name of the histogram is | 172 // Log the Most Visited navigation for navigations that have providers and |
142 // generated dynamically. | 173 // those that dont. |
143 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( | 174 UMA_HISTOGRAM_ENUMERATION(kMostVisitedImpressionHistogramName, position, |
144 base::StringPrintf(kImpressionHistogramWithProvider, | 175 kNumMostVisited); |
145 base::UTF16ToUTF8(provider).c_str()), | 176 |
146 1, MostVisitedIframeSource::kNumMostVisited, | 177 // If a provider is specified, log the metric specific to it. |
147 MostVisitedIframeSource::kNumMostVisited + 1, | 178 if (!provider.empty()) { |
148 base::Histogram::kUmaTargetedHistogramFlag); | 179 // Cannot rely on UMA histograms macro because the name of the histogram is |
149 counter->Add(position); | 180 // generated dynamically. |
| 181 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( |
| 182 GetMostVisitedImpressionHistogramNameForProvider( |
| 183 base::UTF16ToUTF8(provider)), |
| 184 1, |
| 185 kNumMostVisited, |
| 186 kNumMostVisited + 1, |
| 187 base::Histogram::kUmaTargetedHistogramFlag); |
| 188 counter->Add(position); |
| 189 } |
| 190 } |
| 191 |
| 192 void NTPUserDataLogger::LogMostVisitedNavigation( |
| 193 int position, const base::string16& provider) { |
| 194 // Log the Most Visited navigation for navigations that have providers and |
| 195 // those that dont. |
| 196 UMA_HISTOGRAM_ENUMERATION(kMostVisitedNavigationHistogramName, position, |
| 197 kNumMostVisited); |
| 198 |
| 199 // If a provider is specified, log the metric specific to it. |
| 200 if (!provider.empty()) { |
| 201 // Cannot rely on UMA histograms macro because the name of the histogram is |
| 202 // generated dynamically. |
| 203 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( |
| 204 GetMostVisitedNavigationHistogramNameForProvider( |
| 205 base::UTF16ToUTF8(provider)), |
| 206 1, |
| 207 kNumMostVisited, |
| 208 kNumMostVisited + 1, |
| 209 base::Histogram::kUmaTargetedHistogramFlag); |
| 210 counter->Add(position); |
| 211 } |
| 212 |
| 213 // Records the action. This will be available as a time-stamped stream |
| 214 // server-side and can be used to compute time-to-long-dwell. |
| 215 content::RecordAction(base::UserMetricsAction("MostVisited_Clicked")); |
150 } | 216 } |
151 | 217 |
152 // content::WebContentsObserver override | 218 // content::WebContentsObserver override |
153 void NTPUserDataLogger::NavigationEntryCommitted( | 219 void NTPUserDataLogger::NavigationEntryCommitted( |
154 const content::LoadCommittedDetails& load_details) { | 220 const content::LoadCommittedDetails& load_details) { |
155 if (!load_details.previous_url.is_valid()) | 221 if (!load_details.previous_url.is_valid()) |
156 return; | 222 return; |
157 | 223 |
158 if (search::MatchesOriginAndPath(ntp_url_, load_details.previous_url)) { | 224 if (search::MatchesOriginAndPath(ntp_url_, load_details.previous_url)) { |
159 EmitNtpStatistics(); | 225 EmitNtpStatistics(); |
160 } | 226 } |
161 } | 227 } |
162 | 228 |
163 NTPUserDataLogger::NTPUserDataLogger(content::WebContents* contents) | 229 NTPUserDataLogger::NTPUserDataLogger(content::WebContents* contents) |
164 : content::WebContentsObserver(contents), | 230 : content::WebContentsObserver(contents), |
165 has_server_side_suggestions_(false), | 231 has_server_side_suggestions_(false), |
166 number_of_tiles_(0), | 232 number_of_tiles_(0), |
167 number_of_thumbnail_tiles_(0), | 233 number_of_thumbnail_tiles_(0), |
168 number_of_gray_tiles_(0), | 234 number_of_gray_tiles_(0), |
169 number_of_external_tiles_(0), | 235 number_of_external_tiles_(0), |
170 number_of_thumbnail_errors_(0), | 236 number_of_thumbnail_errors_(0), |
171 number_of_gray_tile_fallbacks_(0), | 237 number_of_gray_tile_fallbacks_(0), |
172 number_of_external_tile_fallbacks_(0), | 238 number_of_external_tile_fallbacks_(0), |
173 number_of_mouseovers_(0) { | 239 number_of_mouseovers_(0) { |
174 } | 240 } |
OLD | NEW |