• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 #include "components/translate/core/browser/translate_browser_metrics.h"
6 
7 #include <string>
8 
9 #include "base/basictypes.h"
10 #include "base/metrics/histogram.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "components/language_usage_metrics/language_usage_metrics.h"
13 
14 namespace translate {
15 
16 namespace {
17 
18 // Constant string values to indicate UMA names. All entries should have
19 // a corresponding index in MetricsNameIndex and an entry in |kMetricsEntries|.
20 const char kTranslateInitiationStatus[] =
21     "Translate.InitiationStatus.v2";
22 const char kTranslateReportLanguageDetectionError[] =
23     "Translate.ReportLanguageDetectionError";
24 const char kTranslateLocalesOnDisabledByPrefs[] =
25     "Translate.LocalesOnDisabledByPrefs";
26 const char kTranslateUndisplayableLanguage[] =
27     "Translate.UndisplayableLanguage";
28 const char kTranslateUnsupportedLanguageAtInitiation[] =
29     "Translate.UnsupportedLanguageAtInitiation";
30 
31 struct MetricsEntry {
32   TranslateBrowserMetrics::MetricsNameIndex index;
33   const char* const name;
34 };
35 
36 // This entry table should be updated when new UMA items are added.
37 const MetricsEntry kMetricsEntries[] = {
38   { TranslateBrowserMetrics::UMA_INITIATION_STATUS,
39     kTranslateInitiationStatus },
40   { TranslateBrowserMetrics::UMA_LANGUAGE_DETECTION_ERROR,
41     kTranslateReportLanguageDetectionError },
42   { TranslateBrowserMetrics::UMA_LOCALES_ON_DISABLED_BY_PREFS,
43     kTranslateLocalesOnDisabledByPrefs },
44   { TranslateBrowserMetrics::UMA_UNDISPLAYABLE_LANGUAGE,
45     kTranslateUndisplayableLanguage },
46   { TranslateBrowserMetrics::UMA_UNSUPPORTED_LANGUAGE_AT_INITIATION,
47     kTranslateUnsupportedLanguageAtInitiation },
48 };
49 
50 COMPILE_ASSERT(arraysize(kMetricsEntries) == TranslateBrowserMetrics::UMA_MAX,
51                arraysize_of_kMetricsEntries_should_be_UMA_MAX);
52 
53 }  // namespace
54 
55 namespace TranslateBrowserMetrics {
56 
ReportInitiationStatus(InitiationStatusType type)57 void ReportInitiationStatus(InitiationStatusType type) {
58   UMA_HISTOGRAM_ENUMERATION(kTranslateInitiationStatus,
59                             type,
60                             INITIATION_STATUS_MAX);
61 }
62 
ReportLanguageDetectionError()63 void ReportLanguageDetectionError() {
64   UMA_HISTOGRAM_BOOLEAN(kTranslateReportLanguageDetectionError, true);
65 }
66 
ReportLocalesOnDisabledByPrefs(const std::string & locale)67 void ReportLocalesOnDisabledByPrefs(const std::string& locale) {
68   UMA_HISTOGRAM_SPARSE_SLOWLY(
69       kTranslateLocalesOnDisabledByPrefs,
70       language_usage_metrics::LanguageUsageMetrics::ToLanguageCode(locale));
71 }
72 
ReportUndisplayableLanguage(const std::string & language)73 void ReportUndisplayableLanguage(const std::string& language) {
74   int language_code =
75       language_usage_metrics::LanguageUsageMetrics::ToLanguageCode(language);
76   UMA_HISTOGRAM_SPARSE_SLOWLY(kTranslateUndisplayableLanguage,
77                               language_code);
78 }
79 
ReportUnsupportedLanguageAtInitiation(const std::string & language)80 void ReportUnsupportedLanguageAtInitiation(const std::string& language) {
81   int language_code =
82       language_usage_metrics::LanguageUsageMetrics::ToLanguageCode(language);
83   UMA_HISTOGRAM_SPARSE_SLOWLY(kTranslateUnsupportedLanguageAtInitiation,
84                               language_code);
85 }
86 
GetMetricsName(MetricsNameIndex index)87 const char* GetMetricsName(MetricsNameIndex index) {
88   for (size_t i = 0; i < arraysize(kMetricsEntries); ++i) {
89     if (kMetricsEntries[i].index == index)
90       return kMetricsEntries[i].name;
91   }
92   NOTREACHED();
93   return NULL;
94 }
95 
96 }  // namespace TranslateBrowserMetrics
97 
98 }  // namespace translate
99