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

ash / quick_insert / metrics / quick_insert_session_metrics.h [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_QUICK_INSERT_METRICS_QUICK_INSERT_SESSION_METRICS_H_
#define ASH_QUICK_INSERT_METRICS_QUICK_INSERT_SESSION_METRICS_H_

#include <optional>
#include <string>

#include "ash/ash_export.h"
#include "ash/quick_insert/quick_insert_category.h"
#include "ash/quick_insert/quick_insert_search_result.h"

namespace ui {
class TextInputClient;
}  // namespace ui

class PrefRegistrySimple;
class PrefService;

namespace ash {

// Records metrics for a session of using Quick Insert.
class ASH_EXPORT QuickInsertSessionMetrics {
 public:
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class SessionOutcome {
    // The outcome is unknown.
    kUnknown = 0,
    // User inserts or copies a result.
    kInsertedOrCopied = 1,
    // User abandons the session (e.g. by closing the window without inserting).
    kAbandoned = 2,
    // User selects an action to open another window, e.g. the Emoji picker.
    kRedirected = 3,
    // User selects an action related to text format.
    kFormat = 4,
    // User opens a file.
    kOpenFile = 5,
    // User opens a link.
    kOpenLink = 6,
    // User creates a google workspace file or webpage.
    kCreate = 7,
    kMaxValue = kCreate,
  };

  QuickInsertSessionMetrics();
  explicit QuickInsertSessionMetrics(PrefService* prefs);
  ~QuickInsertSessionMetrics();

  // Registers prefs to the provided `registry`.
  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

  // Sets session outcome. This is expected to be called exactly once during
  // a session.
  void SetOutcome(SessionOutcome outcome);

  // Sets the last category selected by the user during the session.
  // This can be multiple times per session. Only the last category is recorded.
  void SetSelectedCategory(QuickInsertCategory category);

  // Sets the search result which user selects to finish the session.
  // This is expected to be called at most once during a session.
  void SetSelectedResult(QuickInsertSearchResult selected_result, int index);

  // Updates the search query to latest and accumulates total edits.
  void UpdateSearchQuery(std::u16string_view search_query);

  // Records CrOS event metrics when a Quick Insert session starts.
  void OnStartSession(ui::TextInputClient* client);

  // Records if caps lock toggle is displayed in the zero state view.
  void SetCapsLockDisplayed(bool displayed);

  SessionOutcome GetOutcomeForTesting() { return outcome_; }

 private:
  // Records CrOS event metrics when a Quick Insert session finishes.
  void OnFinishSession();

  // Updates caps lock related prefs.
  void UpdateCapLockPrefs(bool caps_lock_selected);

  SessionOutcome outcome_ = SessionOutcome::kUnknown;

  std::optional<QuickInsertCategory> last_category_;

  std::optional<QuickInsertSearchResult> selected_result_;
  int result_index_ = -1;

  int search_query_total_edits_ = 0;
  int search_query_length_ = 0;

  bool caps_lock_displayed_ = false;

  raw_ptr<PrefService> prefs_;
};

}  // namespace ash

#endif  // ASH_QUICK_INSERT_METRICS_QUICK_INSERT_SESSION_METRICS_H_