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