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
3// found in the LICENSE file.
4
5#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_METRICS_H_
6#define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_METRICS_H_
7
8#include <stddef.h>
9#include <string>
10
11#include "base/basictypes.h"
12#include "components/autofill/core/browser/autofill_client.h"
13#include "components/autofill/core/browser/field_types.h"
14
15namespace base {
16class TimeDelta;
17}
18
19namespace autofill {
20
21class AutofillMetrics {
22 public:
23  enum DeveloperEngagementMetric {
24    // Parsed a form that is potentially autofillable.
25    FILLABLE_FORM_PARSED = 0,
26    // Parsed a form that is potentially autofillable and contains at least one
27    // web developer-specified field type hint, a la
28    // http://is.gd/whatwg_autocomplete
29    FILLABLE_FORM_CONTAINS_TYPE_HINTS,
30    NUM_DEVELOPER_ENGAGEMENT_METRICS,
31  };
32
33  // The action the user took to dismiss a dialog.
34  enum DialogDismissalAction {
35    DIALOG_ACCEPTED = 0,  // The user accepted, i.e. submitted, the dialog.
36    DIALOG_CANCELED,      // The user canceled out of the dialog.
37  };
38
39  // The state of the Autofill dialog when it was dismissed.
40  enum DialogDismissalState {
41    // The user submitted with no data available to save.
42    DEPRECATED_DIALOG_ACCEPTED_EXISTING_DATA,
43    // The saved details to Online Wallet on submit.
44    DIALOG_ACCEPTED_SAVE_TO_WALLET,
45    // The saved details to the local Autofill database on submit.
46    DIALOG_ACCEPTED_SAVE_TO_AUTOFILL,
47    // The user submitted without saving any edited sections.
48    DIALOG_ACCEPTED_NO_SAVE,
49    // The user canceled with no edit UI showing.
50    DIALOG_CANCELED_NO_EDITS,
51    // The user canceled with edit UI showing, but no invalid fields.
52    DIALOG_CANCELED_NO_INVALID_FIELDS,
53    // The user canceled with at least one invalid field.
54    DIALOG_CANCELED_WITH_INVALID_FIELDS,
55    // The user canceled while the sign-in form was showing.
56    DIALOG_CANCELED_DURING_SIGNIN,
57    // The user submitted using data already stored in Wallet.
58    DIALOG_ACCEPTED_EXISTING_WALLET_DATA,
59    // The user submitted using data already stored in Autofill.
60    DIALOG_ACCEPTED_EXISTING_AUTOFILL_DATA,
61    NUM_DIALOG_DISMISSAL_STATES
62  };
63
64  // The initial state of user that's interacting with a freshly shown Autofill
65  // dialog.
66  enum DialogInitialUserStateMetric {
67    // Could not determine the user's state due to failure to communicate with
68    // the Wallet server.
69    DIALOG_USER_STATE_UNKNOWN = 0,
70    // Not signed in, no verified Autofill profiles.
71    DIALOG_USER_NOT_SIGNED_IN_NO_AUTOFILL,
72    // Not signed in, has verified Autofill profiles.
73    DIALOG_USER_NOT_SIGNED_IN_HAS_AUTOFILL,
74    // Signed in, no Wallet items, no verified Autofill profiles.
75    DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL,
76    // Signed in, no Wallet items, has verified Autofill profiles.
77    DIALOG_USER_SIGNED_IN_NO_WALLET_HAS_AUTOFILL,
78    // Signed in, has Wallet items, no verified Autofill profiles.
79    DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL,
80    // Signed in, has Wallet items, has verified Autofill profiles.
81    DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL,
82    NUM_DIALOG_INITIAL_USER_STATE_METRICS
83  };
84
85  // Events related to the Autofill popup shown in a requestAutocomplete
86  // dialog.
87  enum DialogPopupEvent {
88    // An Autofill popup was shown.
89    DIALOG_POPUP_SHOWN = 0,
90    // The user chose to fill the form with a suggestion from the popup.
91    DIALOG_POPUP_FORM_FILLED,
92    NUM_DIALOG_POPUP_EVENTS
93  };
94
95  // For measuring the frequency of security warnings or errors that can come
96  // up as part of the requestAutocomplete flow.
97  enum DialogSecurityMetric {
98    // Baseline metric: The dialog was shown.
99    SECURITY_METRIC_DIALOG_SHOWN = 0,
100    // Credit card requested over non-secure protocol.
101    SECURITY_METRIC_CREDIT_CARD_OVER_HTTP,
102    // Autocomplete data requested from a frame hosted on an origin not matching
103    // the main frame's origin.
104    SECURITY_METRIC_CROSS_ORIGIN_FRAME,
105    NUM_DIALOG_SECURITY_METRICS
106  };
107
108  // For measuring how users are interacting with the Autofill dialog UI.
109  enum DialogUiEvent {
110    // Baseline metric: The dialog was shown.
111    DIALOG_UI_SHOWN = 0,
112
113    // Dialog dismissal actions:
114    DIALOG_UI_ACCEPTED,
115    DIALOG_UI_CANCELED,
116
117    // Selections within the account switcher:
118    // Switched from a Wallet account to local Autofill data.
119    DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL,
120    // Switched from local Autofill data to a Wallet account.
121    DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET,
122    // Switched from one Wallet account to another one.
123    DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT,
124
125    // The sign-in UI was shown.
126    DIALOG_UI_SIGNIN_SHOWN,
127
128    // Selecting a different item from a suggestion menu dropdown:
129    DEPRECATED_DIALOG_UI_EMAIL_SELECTED_SUGGESTION_CHANGED,
130    DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED,
131    DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED,
132    DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED,
133    DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED,
134
135    // Showing the editing UI for a section of the dialog:
136    DEPRECATED_DIALOG_UI_EMAIL_EDIT_UI_SHOWN,
137    DEPRECATED_DIALOG_UI_BILLING_EDIT_UI_SHOWN,
138    DEPRECATED_DIALOG_UI_CC_BILLING_EDIT_UI_SHOWN,
139    DEPRECATED_DIALOG_UI_SHIPPING_EDIT_UI_SHOWN,
140    DEPRECATED_DIALOG_UI_CC_EDIT_UI_SHOWN,
141
142    // Adding a new item in a section of the dialog:
143    DEPRECATED_DIALOG_UI_EMAIL_ITEM_ADDED,
144    DIALOG_UI_BILLING_ITEM_ADDED,
145    DIALOG_UI_CC_BILLING_ITEM_ADDED,
146    DIALOG_UI_SHIPPING_ITEM_ADDED,
147    DIALOG_UI_CC_ITEM_ADDED,
148
149    // Also an account switcher menu item. The user selected the
150    // "add account" option.
151    DIALOG_UI_ACCOUNT_CHOOSER_TRIED_TO_ADD_ACCOUNT,
152
153    NUM_DIALOG_UI_EVENTS
154  };
155
156  enum InfoBarMetric {
157    INFOBAR_SHOWN = 0,  // We showed an infobar, e.g. prompting to save credit
158                        // card info.
159    INFOBAR_ACCEPTED,   // The user explicitly accepted the infobar.
160    INFOBAR_DENIED,     // The user explicitly denied the infobar.
161    INFOBAR_IGNORED,    // The user completely ignored the infobar (logged on
162                        // tab close).
163    NUM_INFO_BAR_METRICS,
164  };
165
166  // Metrics measuring how well we predict field types.  Exactly three such
167  // metrics are logged for each fillable field in a submitted form: for
168  // the heuristic prediction, for the crowd-sourced prediction, and for the
169  // overall prediction.
170  enum FieldTypeQualityMetric {
171    TYPE_UNKNOWN = 0,  // Offered no prediction.
172    TYPE_MATCH,        // Predicted correctly.
173    TYPE_MISMATCH,     // Predicted incorrectly.
174    NUM_FIELD_TYPE_QUALITY_METRICS,
175  };
176
177  // Each of these is logged at most once per query to the server, which in turn
178  // occurs at most once per page load.
179  enum ServerQueryMetric {
180    QUERY_SENT = 0,           // Sent a query to the server.
181    QUERY_RESPONSE_RECEIVED,  // Received a response.
182    QUERY_RESPONSE_PARSED,    // Successfully parsed the server response.
183
184    // The response was parseable, but provided no improvements relative to our
185    // heuristics.
186    QUERY_RESPONSE_MATCHED_LOCAL_HEURISTICS,
187
188    // Our heuristics detected at least one auto-fillable field, and the server
189    // response overrode the type of at least one field.
190    QUERY_RESPONSE_OVERRODE_LOCAL_HEURISTICS,
191
192    // Our heuristics did not detect any auto-fillable fields, but the server
193    // response did detect at least one.
194    QUERY_RESPONSE_WITH_NO_LOCAL_HEURISTICS,
195    NUM_SERVER_QUERY_METRICS,
196  };
197
198  // Each of these metrics is logged only for potentially autofillable forms,
199  // i.e. forms with at least three fields, etc.
200  // These are used to derive certain "user happiness" metrics.  For example, we
201  // can compute the ratio (USER_DID_EDIT_AUTOFILLED_FIELD / USER_DID_AUTOFILL)
202  // to see how often users have to correct autofilled data.
203  enum UserHappinessMetric {
204    // Loaded a page containing forms.
205    FORMS_LOADED,
206    // Submitted a fillable form -- i.e. one with at least three field values
207    // that match the user's stored Autofill data -- and all matching fields
208    // were autofilled.
209    SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL,
210    // Submitted a fillable form and some (but not all) matching fields were
211    // autofilled.
212    SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME,
213    // Submitted a fillable form and no fields were autofilled.
214    SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE,
215    // Submitted a non-fillable form.
216    SUBMITTED_NON_FILLABLE_FORM,
217
218    // User manually filled one of the form fields.
219    USER_DID_TYPE,
220    // We showed a popup containing Autofill suggestions.
221    SUGGESTIONS_SHOWN,
222    // Same as above, but only logged once per page load.
223    SUGGESTIONS_SHOWN_ONCE,
224    // User autofilled at least part of the form.
225    USER_DID_AUTOFILL,
226    // Same as above, but only logged once per page load.
227    USER_DID_AUTOFILL_ONCE,
228    // User edited a previously autofilled field.
229    USER_DID_EDIT_AUTOFILLED_FIELD,
230    // Same as above, but only logged once per page load.
231    USER_DID_EDIT_AUTOFILLED_FIELD_ONCE,
232    NUM_USER_HAPPINESS_METRICS,
233  };
234
235  // For measuring the network request time of various Wallet API calls. See
236  // WalletClient::RequestType.
237  enum WalletApiCallMetric {
238    UNKNOWN_API_CALL,  // Catch all. Should never be used.
239    ACCEPT_LEGAL_DOCUMENTS,
240    AUTHENTICATE_INSTRUMENT,
241    GET_FULL_WALLET,
242    GET_WALLET_ITEMS,
243    SAVE_TO_WALLET,
244    NUM_WALLET_API_CALLS
245  };
246
247  // For measuring the frequency of errors while communicating with the Wallet
248  // server.
249  enum WalletErrorMetric {
250    // Baseline metric: Issued a request to the Wallet server.
251    WALLET_ERROR_BASELINE_ISSUED_REQUEST = 0,
252    // A fatal error occured while communicating with the Wallet server. This
253    // value has been deprecated.
254    WALLET_FATAL_ERROR_DEPRECATED,
255    // Received a malformed response from the Wallet server.
256    WALLET_MALFORMED_RESPONSE,
257    // A network error occured while communicating with the Wallet server.
258    WALLET_NETWORK_ERROR,
259    // The request was malformed.
260    WALLET_BAD_REQUEST,
261    // Risk deny, unsupported country, or account closed.
262    WALLET_BUYER_ACCOUNT_ERROR,
263    // Unknown server side error.
264    WALLET_INTERNAL_ERROR,
265    // API call had missing or invalid parameters.
266    WALLET_INVALID_PARAMS,
267    // Online Wallet is down.
268    WALLET_SERVICE_UNAVAILABLE,
269    // User needs make a cheaper transaction or not use Online Wallet.
270    WALLET_SPENDING_LIMIT_EXCEEDED,
271    // The server API version of the request is no longer supported.
272    WALLET_UNSUPPORTED_API_VERSION,
273    // Catch all error type.
274    WALLET_UNKNOWN_ERROR,
275    // The merchant has been blacklisted for Online Wallet due to some manner
276    // of compliance violation.
277    WALLET_UNSUPPORTED_MERCHANT,
278    // Buyer Legal Address has a country which is unsupported by Wallet.
279    WALLET_BUYER_LEGAL_ADDRESS_NOT_SUPPORTED,
280    // Wallet's Know Your Customer(KYC) action is pending/failed for this user.
281    WALLET_UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS,
282    // Chrome version is unsupported or provided API key not allowed.
283    WALLET_UNSUPPORTED_USER_AGENT_OR_API_KEY,
284    NUM_WALLET_ERROR_METRICS
285  };
286
287  // For measuring the frequency of "required actions" returned by the Wallet
288  // server.  This is similar to the autofill::wallet::RequiredAction enum;
289  // but unlike that enum, the values in this one must remain constant over
290  // time, so that the metrics can be consistently interpreted on the
291  // server-side.
292  enum WalletRequiredActionMetric {
293    // Baseline metric: Issued a request to the Wallet server.
294    WALLET_REQUIRED_ACTION_BASELINE_ISSUED_REQUEST = 0,
295    // Values from the autofill::wallet::RequiredAction enum:
296    UNKNOWN_REQUIRED_ACTION,  // Catch all type.
297    GAIA_AUTH,
298    PASSIVE_GAIA_AUTH,
299    SETUP_WALLET,
300    ACCEPT_TOS,
301    UPDATE_EXPIRATION_DATE,
302    UPGRADE_MIN_ADDRESS,
303    CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS,
304    VERIFY_CVV,
305    INVALID_FORM_FIELD,
306    REQUIRE_PHONE_NUMBER,
307    NUM_WALLET_REQUIRED_ACTIONS
308  };
309
310  AutofillMetrics();
311  virtual ~AutofillMetrics();
312
313  virtual void LogCreditCardInfoBarMetric(InfoBarMetric metric) const;
314
315  virtual void LogDeveloperEngagementMetric(
316      DeveloperEngagementMetric metric) const;
317
318  virtual void LogHeuristicTypePrediction(FieldTypeQualityMetric metric,
319                                          ServerFieldType field_type) const;
320  virtual void LogOverallTypePrediction(FieldTypeQualityMetric metric,
321                                        ServerFieldType field_type) const;
322  virtual void LogServerTypePrediction(FieldTypeQualityMetric metric,
323                                       ServerFieldType field_type) const;
324
325  virtual void LogServerQueryMetric(ServerQueryMetric metric) const;
326
327  virtual void LogUserHappinessMetric(UserHappinessMetric metric) const;
328
329  // Logs |state| to the dismissal states histogram.
330  virtual void LogDialogDismissalState(DialogDismissalState state) const;
331
332  // This should be called as soon as the user's signed-in status and Wallet
333  // item count is known.  Records that a user starting out in |user_state| is
334  // interacting with a dialog.
335  virtual void LogDialogInitialUserState(
336      DialogInitialUserStateMetric user_type) const;
337
338  // Logs the time elapsed between the dialog being shown and when it is ready
339  // for user interaction.
340  virtual void LogDialogLatencyToShow(const base::TimeDelta& duration) const;
341
342  // Logs |event| to the popup events histogram.
343  virtual void LogDialogPopupEvent(DialogPopupEvent event) const;
344
345  // Logs |metric| to the security metrics histogram.
346  virtual void LogDialogSecurityMetric(DialogSecurityMetric metric) const;
347
348  // This should be called when the Autofill dialog is closed.  |duration|
349  // should be the time elapsed between the dialog being shown and it being
350  // closed.  |dismissal_action| should indicate whether the user dismissed
351  // the dialog by submitting the form data or by canceling.
352  virtual void LogDialogUiDuration(
353      const base::TimeDelta& duration,
354      DialogDismissalAction dismissal_action) const;
355
356  // Logs |event| to the UI events histogram.
357  virtual void LogDialogUiEvent(DialogUiEvent event) const;
358
359  // Logs |metric| to the Wallet errors histogram.
360  virtual void LogWalletErrorMetric(WalletErrorMetric metric) const;
361
362  // Logs the network request time of Wallet API calls.
363  virtual void LogWalletApiCallDuration(
364      WalletApiCallMetric metric,
365      const base::TimeDelta& duration) const;
366
367  // Logs that the Wallet API call corresponding to |metric| was malformed.
368  virtual void LogWalletMalformedResponseMetric(
369      WalletApiCallMetric metric) const;
370
371  // Logs |required_action| to the required actions histogram.
372  virtual void LogWalletRequiredActionMetric(
373      WalletRequiredActionMetric required_action) const;
374
375  // Logs HTTP response codes recieved by wallet client.
376  virtual void LogWalletResponseCode(int response_code) const;
377
378  // This should be called when a form that has been Autofilled is submitted.
379  // |duration| should be the time elapsed between form load and submission.
380  virtual void LogFormFillDurationFromLoadWithAutofill(
381      const base::TimeDelta& duration) const;
382
383  // This should be called when a fillable form that has not been Autofilled is
384  // submitted.  |duration| should be the time elapsed between form load and
385  // submission.
386  virtual void LogFormFillDurationFromLoadWithoutAutofill(
387      const base::TimeDelta& duration) const;
388
389  // This should be called when a form that has been Autofilled is submitted.
390  // |duration| should be the time elapsed between the initial form interaction
391  // and submission.
392  virtual void LogFormFillDurationFromInteractionWithAutofill(
393      const base::TimeDelta& duration) const;
394
395  // This should be called when a fillable form that has not been Autofilled is
396  // submitted.  |duration| should be the time elapsed between the initial form
397  // interaction and submission.
398  virtual void LogFormFillDurationFromInteractionWithoutAutofill(
399      const base::TimeDelta& duration) const;
400
401  // This should be called each time a page containing forms is loaded.
402  virtual void LogIsAutofillEnabledAtPageLoad(bool enabled) const;
403
404  // This should be called each time a new profile is launched.
405  virtual void LogIsAutofillEnabledAtStartup(bool enabled) const;
406
407  // This should be called each time a new profile is launched.
408  virtual void LogStoredProfileCount(size_t num_profiles) const;
409
410  // Log the number of Autofill suggestions presented to the user when filling a
411  // form.
412  virtual void LogAddressSuggestionsCount(size_t num_suggestions) const;
413
414 private:
415  DISALLOW_COPY_AND_ASSIGN(AutofillMetrics);
416};
417
418}  // namespace autofill
419
420#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_METRICS_H_
421