OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/ntp_tiles/metrics.h" | 5 #include "components/ntp_tiles/metrics.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
11 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "components/rappor/public/rappor_utils.h" | 13 #include "components/rappor/public/rappor_utils.h" |
14 | 14 |
15 namespace ntp_tiles { | 15 namespace ntp_tiles { |
16 namespace metrics { | 16 namespace metrics { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Maximum number of tiles to record in histograms. | 20 // Maximum number of tiles to record in histograms. |
21 const int kMaxNumTiles = 12; | 21 const int kMaxNumTiles = 12; |
22 | 22 |
23 // Identifiers for the various tile sources. | 23 // Identifiers for the various tile sources. |
24 const char kHistogramClientName[] = "client"; | 24 const char kHistogramClientName[] = "client"; |
25 const char kHistogramServerName[] = "server"; | 25 const char kHistogramServerName[] = "server"; |
26 const char kHistogramPopularName[] = "popular"; | 26 const char kHistogramPopularName[] = "popular"; |
27 const char kHistogramWhitelistName[] = "whitelist"; | 27 const char kHistogramWhitelistName[] = "whitelist"; |
28 | 28 |
29 // Suffixes for the various icon types. | 29 // Suffixes for the various icon types. |
30 const char kIconTypeSuffixColor[] = "IconsColor"; | 30 const char kTileTypeSuffixIconColor[] = "IconsColor"; |
31 const char kIconTypeSuffixGray[] = "IconsGray"; | 31 const char kTileTypeSuffixIconGray[] = "IconsGray"; |
32 const char kIconTypeSuffixReal[] = "IconsReal"; | 32 const char kTileTypeSuffixIconReal[] = "IconsReal"; |
| 33 const char kTileTypeSuffixThumbnail[] = "Thumbnail"; |
| 34 const char kTileTypeSuffixThumbnailFailed[] = "ThumbnailFailed"; |
33 | 35 |
34 // Log an event for a given |histogram| at a given element |position|. This | 36 // Log an event for a given |histogram| at a given element |position|. This |
35 // routine exists because regular histogram macros are cached thus can't be used | 37 // routine exists because regular histogram macros are cached thus can't be used |
36 // if the name of the histogram will change at a given call site. | 38 // if the name of the histogram will change at a given call site. |
37 void LogHistogramEvent(const std::string& histogram, | 39 void LogHistogramEvent(const std::string& histogram, |
38 int position, | 40 int position, |
39 int num_sites) { | 41 int num_sites) { |
40 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( | 42 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( |
41 histogram, 1, num_sites, num_sites + 1, | 43 histogram, 1, num_sites, num_sites + 1, |
42 base::Histogram::kUmaTargetedHistogramFlag); | 44 base::Histogram::kUmaTargetedHistogramFlag); |
43 if (counter) | 45 if (counter) |
44 counter->Add(position); | 46 counter->Add(position); |
45 } | 47 } |
46 | 48 |
47 std::string GetSourceHistogramName(TileSource source) { | 49 std::string GetSourceHistogramName(TileSource source) { |
48 switch (source) { | 50 switch (source) { |
49 case TileSource::TOP_SITES: | 51 case TileSource::TOP_SITES: |
50 return kHistogramClientName; | 52 return kHistogramClientName; |
51 case TileSource::POPULAR: | 53 case TileSource::POPULAR: |
52 return kHistogramPopularName; | 54 return kHistogramPopularName; |
53 case TileSource::WHITELIST: | 55 case TileSource::WHITELIST: |
54 return kHistogramWhitelistName; | 56 return kHistogramWhitelistName; |
55 case TileSource::SUGGESTIONS_SERVICE: | 57 case TileSource::SUGGESTIONS_SERVICE: |
56 return kHistogramServerName; | 58 return kHistogramServerName; |
57 } | 59 } |
58 NOTREACHED(); | 60 NOTREACHED(); |
59 return std::string(); | 61 return std::string(); |
60 } | 62 } |
61 | 63 |
62 const char* GetIconTypeSuffix(TileVisualType type) { | 64 const char* GetTileTypeSuffix(TileVisualType type) { |
63 switch (type) { | 65 switch (type) { |
64 case TileVisualType::ICON_COLOR: | 66 case TileVisualType::ICON_COLOR: |
65 return kIconTypeSuffixColor; | 67 return kTileTypeSuffixIconColor; |
66 case TileVisualType::ICON_DEFAULT: | 68 case TileVisualType::ICON_DEFAULT: |
67 return kIconTypeSuffixGray; | 69 return kTileTypeSuffixIconGray; |
68 case TileVisualType::ICON_REAL: | 70 case TileVisualType::ICON_REAL: |
69 return kIconTypeSuffixReal; | 71 return kTileTypeSuffixIconReal; |
| 72 case THUMBNAIL: |
| 73 return kTileTypeSuffixThumbnail; |
| 74 case THUMBNAIL_FAILED: |
| 75 return kTileTypeSuffixThumbnailFailed; |
70 case TileVisualType::NONE: // Fall through. | 76 case TileVisualType::NONE: // Fall through. |
71 case TileVisualType::NUM_RECORDED_TILE_TYPES: // Fall through. | |
72 case TileVisualType::THUMBNAIL: // Fall through. | |
73 case TileVisualType::UNKNOWN_TILE_TYPE: | 77 case TileVisualType::UNKNOWN_TILE_TYPE: |
74 break; | 78 break; |
75 } | 79 } |
76 return nullptr; | 80 return nullptr; |
77 } | 81 } |
78 | 82 |
79 } // namespace | 83 } // namespace |
80 | 84 |
81 void RecordPageImpression(const std::vector<TileImpression>& tiles, | 85 void RecordPageImpression(const std::vector<TileImpression>& tiles, |
82 rappor::RapporService* rappor_service) { | 86 rappor::RapporService* rappor_service) { |
83 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", tiles.size()); | 87 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", tiles.size()); |
84 | 88 |
85 for (int index = 0; index < static_cast<int>(tiles.size()); index++) { | 89 for (int index = 0; index < static_cast<int>(tiles.size()); index++) { |
86 TileSource source = tiles[index].source; | 90 TileSource source = tiles[index].source; |
87 TileVisualType tile_type = tiles[index].type; | 91 TileVisualType tile_type = tiles[index].type; |
88 const GURL& url = tiles[index].url; | |
89 | 92 |
90 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SuggestionsImpression", index, | 93 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SuggestionsImpression", index, |
91 kMaxNumTiles); | 94 kMaxNumTiles); |
92 | 95 |
93 std::string source_name = GetSourceHistogramName(source); | 96 std::string source_name = GetSourceHistogramName(source); |
94 std::string impression_histogram = base::StringPrintf( | 97 std::string impression_histogram = base::StringPrintf( |
95 "NewTabPage.SuggestionsImpression.%s", source_name.c_str()); | 98 "NewTabPage.SuggestionsImpression.%s", source_name.c_str()); |
96 LogHistogramEvent(impression_histogram, index, kMaxNumTiles); | 99 LogHistogramEvent(impression_histogram, index, kMaxNumTiles); |
97 | 100 |
98 if (tile_type >= NUM_RECORDED_TILE_TYPES) { | 101 if (tile_type > LAST_RECORDED_TILE_TYPE) { |
99 continue; | 102 continue; |
100 } | 103 } |
101 | 104 |
102 UMA_HISTOGRAM_ENUMERATION("NewTabPage.TileType", tile_type, | 105 UMA_HISTOGRAM_ENUMERATION("NewTabPage.TileType", tile_type, |
103 NUM_RECORDED_TILE_TYPES); | 106 LAST_RECORDED_TILE_TYPE + 1); |
104 | 107 |
105 std::string tile_type_histogram = | 108 std::string tile_type_histogram = |
106 base::StringPrintf("NewTabPage.TileType.%s", source_name.c_str()); | 109 base::StringPrintf("NewTabPage.TileType.%s", source_name.c_str()); |
107 LogHistogramEvent(tile_type_histogram, tile_type, NUM_RECORDED_TILE_TYPES); | 110 LogHistogramEvent(tile_type_histogram, tile_type, |
| 111 LAST_RECORDED_TILE_TYPE + 1); |
108 | 112 |
109 const char* icon_type_suffix = GetIconTypeSuffix(tile_type); | 113 const char* tile_type_suffix = GetTileTypeSuffix(tile_type); |
110 if (icon_type_suffix) { | 114 if (tile_type_suffix) { |
| 115 // Note: This handles a null |rappor_service|. |
111 rappor::SampleDomainAndRegistryFromGURL( | 116 rappor::SampleDomainAndRegistryFromGURL( |
112 rappor_service, | 117 rappor_service, |
113 base::StringPrintf("NTP.SuggestionsImpressions.%s", icon_type_suffix), | 118 base::StringPrintf("NTP.SuggestionsImpressions.%s", tile_type_suffix), |
114 url); | 119 tiles[index].url); |
115 | 120 |
116 std::string icon_impression_histogram = base::StringPrintf( | 121 std::string icon_impression_histogram = base::StringPrintf( |
117 "NewTabPage.SuggestionsImpression.%s", icon_type_suffix); | 122 "NewTabPage.SuggestionsImpression.%s", tile_type_suffix); |
118 LogHistogramEvent(icon_impression_histogram, index, kMaxNumTiles); | 123 LogHistogramEvent(icon_impression_histogram, index, kMaxNumTiles); |
119 } | 124 } |
120 } | 125 } |
121 } | 126 } |
122 | 127 |
123 void RecordTileClick(int index, TileSource source, TileVisualType tile_type) { | 128 void RecordTileClick(int index, TileSource source, TileVisualType tile_type) { |
124 UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisited", index, kMaxNumTiles); | 129 UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisited", index, kMaxNumTiles); |
125 | 130 |
126 std::string histogram = base::StringPrintf( | 131 std::string histogram = base::StringPrintf( |
127 "NewTabPage.MostVisited.%s", GetSourceHistogramName(source).c_str()); | 132 "NewTabPage.MostVisited.%s", GetSourceHistogramName(source).c_str()); |
128 LogHistogramEvent(histogram, index, kMaxNumTiles); | 133 LogHistogramEvent(histogram, index, kMaxNumTiles); |
129 | 134 |
130 const char* icon_type_suffix = GetIconTypeSuffix(tile_type); | 135 const char* tile_type_suffix = GetTileTypeSuffix(tile_type); |
131 if (icon_type_suffix) { | 136 if (tile_type_suffix) { |
132 std::string icon_histogram = | 137 std::string tile_type_histogram = |
133 base::StringPrintf("NewTabPage.MostVisited.%s", icon_type_suffix); | 138 base::StringPrintf("NewTabPage.MostVisited.%s", tile_type_suffix); |
134 LogHistogramEvent(icon_histogram, index, kMaxNumTiles); | 139 LogHistogramEvent(tile_type_histogram, index, kMaxNumTiles); |
135 } | 140 } |
136 | 141 |
137 if (tile_type < NUM_RECORDED_TILE_TYPES) { | 142 if (tile_type <= LAST_RECORDED_TILE_TYPE) { |
138 UMA_HISTOGRAM_ENUMERATION("NewTabPage.TileTypeClicked", tile_type, | 143 UMA_HISTOGRAM_ENUMERATION("NewTabPage.TileTypeClicked", tile_type, |
139 NUM_RECORDED_TILE_TYPES); | 144 LAST_RECORDED_TILE_TYPE + 1); |
140 | 145 |
141 std::string histogram = | 146 std::string histogram = |
142 base::StringPrintf("NewTabPage.TileTypeClicked.%s", | 147 base::StringPrintf("NewTabPage.TileTypeClicked.%s", |
143 GetSourceHistogramName(source).c_str()); | 148 GetSourceHistogramName(source).c_str()); |
144 LogHistogramEvent(histogram, tile_type, NUM_RECORDED_TILE_TYPES); | 149 LogHistogramEvent(histogram, tile_type, LAST_RECORDED_TILE_TYPE + 1); |
145 } | 150 } |
146 } | 151 } |
147 | 152 |
148 } // namespace metrics | 153 } // namespace metrics |
149 } // namespace ntp_tiles | 154 } // namespace ntp_tiles |
OLD | NEW |