File: metrics.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (145 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download
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
// 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";

// 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();
}

}  // 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);

    switch (tile_type) {
      case NONE:
        break;
      case ICON_COLOR:
        rappor::SampleDomainAndRegistryFromGURL(
            rappor_service, "NTP.SuggestionsImpressions.IconsColor", url);
        break;
      case ICON_DEFAULT:
        rappor::SampleDomainAndRegistryFromGURL(
            rappor_service, "NTP.SuggestionsImpressions.IconsGray", url);
        break;
      case ICON_REAL:
        rappor::SampleDomainAndRegistryFromGURL(
            rappor_service, "NTP.SuggestionsImpressions.IconsReal", url);
        break;
      case NUM_RECORDED_TILE_TYPES:  // Fall through.
      case THUMBNAIL:  // Fall through.
      case UNKNOWN_TILE_TYPE:
        NOTREACHED();
    }
  }

  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);

  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