File: quick_pair_metrics_logger.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (568 lines) | stat: -rw-r--r-- 24,186 bytes parent folder | download | duplicates (6)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/keyed_service/quick_pair_metrics_logger.h"

#include "ash/constants/ash_pref_names.h"
#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/common/fast_pair/fast_pair_feature_usage_metrics_logger.h"
#include "ash/quick_pair/common/fast_pair/fast_pair_metrics.h"
#include "ash/quick_pair/common/pair_failure.h"
#include "ash/quick_pair/repository/fast_pair/device_metadata.h"
#include "ash/quick_pair/repository/fast_pair_repository.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "base/containers/contains.h"
#include "components/cross_device/logging/logging.h"
#include "components/prefs/pref_service.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"

namespace ash {
namespace quick_pair {

namespace {

void AttemptToRecordEngagementFunnelFlowWithMetadata(
    scoped_refptr<Device> device,
    FastPairEngagementFlowEvent event,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  // TODO(b/262452942): Add logic to retry fetching metadata to record funnel
  // flow. Currently we are missing logging if fetching metadata fails, and we
  // do not retry. |has_retryable_error| is currently not used, but will be if
  // we decide to retry.
  if (!device_metadata) {
    return;
  }

  RecordFastPairDeviceAndNotificationSpecificEngagementFlow(
      *device, device_metadata->GetDetails(), event);
}

void GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
    scoped_refptr<Device> device,
    FastPairEngagementFlowEvent event) {
  FastPairRepository::Get()->GetDeviceMetadata(
      device->metadata_id(),
      base::BindOnce(&AttemptToRecordEngagementFunnelFlowWithMetadata, device,
                     event));
}

void AttemptToRecordRetroactiveEngagementFunnelFlowWithMetadata(
    scoped_refptr<Device> device,
    FastPairRetroactiveEngagementFlowEvent event,
    DeviceMetadata* device_metadata,
    bool has_retryable_error) {
  // TODO(b/262452942): Add logic to retry fetching metadata to record funnel
  // flow. Currently we are missing logging if fetching metadata fails, and we
  // do not retry.
  if (!device_metadata) {
    return;
  }

  RecordFastPairDeviceAndNotificationSpecificRetroactiveEngagementFlow(
      *device, device_metadata->GetDetails(), event);
}

void GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
    scoped_refptr<Device> device,
    FastPairRetroactiveEngagementFlowEvent event) {
  FastPairRepository::Get()->GetDeviceMetadata(
      device->metadata_id(),
      base::BindOnce(
          &AttemptToRecordRetroactiveEngagementFunnelFlowWithMetadata, device,
          event));
}

PrefService* GetLastActiveUserPrefService() {
  return Shell::Get()->session_controller()->GetLastActiveUserPrefService();
}

}  // namespace

QuickPairMetricsLogger::QuickPairMetricsLogger(
    ScannerBroker* scanner_broker,
    PairerBroker* pairer_broker,
    UIBroker* ui_broker,
    RetroactivePairingDetector* retroactive_pairing_detector)
    : feature_usage_metrics_logger_(
          std::make_unique<FastPairFeatureUsageMetricsLogger>()) {
  device::BluetoothAdapterFactory::Get()->GetAdapter(base::BindOnce(
      &QuickPairMetricsLogger::OnGetAdapter, weak_ptr_factory_.GetWeakPtr()));

  scanner_broker_observation_.Observe(scanner_broker);
  retroactive_pairing_detector_observation_.Observe(
      retroactive_pairing_detector);
  pairer_broker_observation_.Observe(pairer_broker);
  ui_broker_observation_.Observe(ui_broker);
}

QuickPairMetricsLogger::~QuickPairMetricsLogger() = default;

void QuickPairMetricsLogger::OnGetAdapter(
    scoped_refptr<device::BluetoothAdapter> adapter) {
  adapter_ = adapter;
  adapter_observation_.Observe(adapter_.get());
}

const device::BluetoothDevice* QuickPairMetricsLogger::GetBluetoothDevice(
    scoped_refptr<Device> device) const {
  if (!adapter_) {
    return nullptr;
  }

  device::BluetoothDevice* bt_device = nullptr;
  // First, try to get the Bluetooth Device via the classic address since it's
  // more stable than the BLE address.
  if (device->classic_address()) {
    bt_device = adapter_->GetDevice(device->classic_address().value());
    CD_LOG(VERBOSE, Feature::FP)
        << __func__
        << ": Structured Classic device found: " << (bt_device ? "yes" : "no");
  }

  if (!bt_device) {
    bt_device = adapter_->GetDevice(device->ble_address());
    CD_LOG(VERBOSE, Feature::FP)
        << __func__
        << ": Structured LE device found: " << (bt_device ? "yes" : "no");
  }

  return bt_device;
}

void QuickPairMetricsLogger::DevicePairedChanged(
    device::BluetoothAdapter* adapter,
    device::BluetoothDevice* device,
    bool new_paired_status) {
  // This event fires whenever a device pairing has changed with the adapter.
  // If the |new_paired_status| is false, it means a device was unpaired with
  // the adapter, so we early return since it would not be a device that has
  // been paired alternatively. If the device that was paired to that fires this
  // event is a device we just paired to with Fast Pair, then we early return
  // since it also wouldn't be one that was alternatively pair to. We want to
  // only continue our check here if we have a newly paired device that was
  // paired with classic Bluetooth pairing.
  const std::string& classic_address = device->GetAddress();
  if (!new_paired_status ||
      base::Contains(fast_pair_addresses_, classic_address)) {
    return;
  }

  RecordPairingMethod(PairingMethod::kSystemPairingUi);
}

void QuickPairMetricsLogger::OnDevicePaired(scoped_refptr<Device> device) {
  AttemptRecordingFastPairEngagementFlow(
      *device, FastPairEngagementFlowEvent::kPairingSucceeded);
  GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
      device, FastPairEngagementFlowEvent::kPairingSucceeded);
  RecordPairingResult(*device, /*success=*/true);
  feature_usage_metrics_logger_->RecordUsage(/*success=*/true);

  base::TimeDelta total_pair_time =
      base::TimeTicks::Now() - device_pairing_start_timestamps_[device];
  AttemptRecordingTotalUxPairTime(*device, total_pair_time);
  RecordPairingMethod(PairingMethod::kFastPair);

  if (device->protocol() == Protocol::kFastPairInitial) {
    RecordInitialSuccessFunnelFlow(
        FastPairInitialSuccessFunnelEvent::kPairingComplete);
  }

  // The classic address is assigned to the Device during the
  // initial Fast Pair pairing protocol during the key exchange, and if it
  // doesn't exist, then it wasn't properly paired during initial Fast Pair
  // pairing. We want to save the addresses here in the event that the
  // Bluetooth adapter pairing event fires, so we can detect when a device
  // was paired solely via classic bluetooth, instead of Fast Pair.
  if (device->classic_address()) {
    fast_pair_addresses_.insert(device->classic_address().value());
  }
}

void QuickPairMetricsLogger::OnPairFailure(scoped_refptr<Device> device,
                                           PairFailure failure) {
  AttemptRecordingFastPairEngagementFlow(
      *device, FastPairEngagementFlowEvent::kPairingFailed);
  GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
      device, FastPairEngagementFlowEvent::kPairingFailed);
  base::TimeDelta total_pair_time =
      base::TimeTicks::Now() - device_pairing_start_timestamps_[device];
  device_pairing_start_timestamps_.erase(device);
  AttemptRecordingTotalUxPairTime(*device, total_pair_time);

  feature_usage_metrics_logger_->RecordUsage(/*success=*/false);
  RecordPairingFailureReason(*device, failure);
  RecordPairingResult(*device, /*success=*/false);
  RecordStructuredPairFailure(*device, failure);
}

void QuickPairMetricsLogger::OnDiscoveryAction(scoped_refptr<Device> device,
                                               DiscoveryAction action) {
  switch (action) {
    case DiscoveryAction::kPairToDevice: {
      switch (device->protocol()) {
        case Protocol::kFastPairSubsequent:
          RecordSubsequentSuccessFunnelFlow(
              FastPairSubsequentSuccessFunnelEvent::kNotificationsClicked);
          RecordStructuredPairingStarted(*device, GetBluetoothDevice(device));
          break;
        case Protocol::kFastPairInitial:
          RecordInitialSuccessFunnelFlow(
              FastPairInitialSuccessFunnelEvent::kNotificationsClicked);
          RecordStructuredPairingStarted(*device, GetBluetoothDevice(device));
          break;
        case Protocol::kFastPairRetroactive:
          break;
      }

      if (base::Contains(discovery_learn_more_devices_, device)) {
        AttemptRecordingFastPairEngagementFlow(
            *device, FastPairEngagementFlowEvent::
                         kDiscoveryUiConnectPressedAfterLearnMorePressed);
        GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
            device, FastPairEngagementFlowEvent::
                        kDiscoveryUiConnectPressedAfterLearnMorePressed);
        discovery_learn_more_devices_.erase(device);
        break;
      }

      PrefService* pref = GetLastActiveUserPrefService();
      if (pref->FindPreference(ash::prefs::kUserPairedWithFastPair)) {
        pref->SetBoolean(ash::prefs::kUserPairedWithFastPair, true);
      }

      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kDiscoveryUiConnectPressed);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kDiscoveryUiConnectPressed);
      device_pairing_start_timestamps_[device] = base::TimeTicks::Now();
      break;
    }
    case DiscoveryAction::kLearnMore:
      // We need to record whether or not the Discovery UI for this
      // device has had the Learn More button pressed because since the
      // Learn More button is not a terminal state, we need to record
      // if the subsequent terminal states were reached after the user
      // has learned more about saving their accounts. So we will check
      // this map when the user dismisses or saves their account in order
      // to capture whether or not the user elected to learn more beforehand.
      discovery_learn_more_devices_.insert(device);

      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kDiscoveryUiLearnMorePressed);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kDiscoveryUiLearnMorePressed);
      break;
    case DiscoveryAction::kDismissedByUser:
      if (base::Contains(discovery_learn_more_devices_, device)) {
        AttemptRecordingFastPairEngagementFlow(
            *device, FastPairEngagementFlowEvent::
                         kDiscoveryUiDismissedByUserAfterLearnMorePressed);
        GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
            device, FastPairEngagementFlowEvent::
                        kDiscoveryUiDismissedByUserAfterLearnMorePressed);
        discovery_learn_more_devices_.erase(device);
        break;
      }

      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kDiscoveryUiDismissedByUser);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kDiscoveryUiDismissedByUser);
      break;
    case DiscoveryAction::kDismissedByOs:
      if (base::Contains(discovery_learn_more_devices_, device)) {
        AttemptRecordingFastPairEngagementFlow(
            *device, FastPairEngagementFlowEvent::
                         kDiscoveryUiDismissedAfterLearnMorePressed);
        GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
            device, FastPairEngagementFlowEvent::
                        kDiscoveryUiDismissedAfterLearnMorePressed);
        discovery_learn_more_devices_.erase(device);
        break;
      }

      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kDiscoveryUiDismissed);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kDiscoveryUiDismissed);
      break;
    case DiscoveryAction::kDismissedByTimeout:
      if (base::Contains(discovery_learn_more_devices_, device)) {
        AttemptRecordingFastPairEngagementFlow(
            *device, FastPairEngagementFlowEvent::
                         kDiscoveryUiDismissedByTimeoutAfterLearnMorePressed);
        GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
            device, FastPairEngagementFlowEvent::
                        kDiscoveryUiDismissedByTimeoutAfterLearnMorePressed);
        discovery_learn_more_devices_.erase(device);
        break;
      }

      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kDiscoveryUiDismissedByTimeout);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kDiscoveryUiDismissedByTimeout);
      break;
  }
}

void QuickPairMetricsLogger::OnPairingFailureAction(
    scoped_refptr<Device> device,
    PairingFailedAction action) {
  switch (action) {
    case PairingFailedAction::kNavigateToSettings:
      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kErrorUiSettingsPressed);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kErrorUiSettingsPressed);
      break;
    case PairingFailedAction::kDismissedByUser:
      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kErrorUiDismissedByUser);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kErrorUiDismissedByUser);
      break;
    case PairingFailedAction::kDismissed:
      AttemptRecordingFastPairEngagementFlow(
          *device, FastPairEngagementFlowEvent::kErrorUiDismissed);
      GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
          device, FastPairEngagementFlowEvent::kErrorUiDismissed);
      break;
  }
}

void QuickPairMetricsLogger::OnDeviceFound(scoped_refptr<Device> device) {
  AttemptRecordingFastPairEngagementFlow(
      *device, FastPairEngagementFlowEvent::kDiscoveryUiShown);
  GetDeviceMetadataAndLogEngagementFunnelWithMetadata(
      device, FastPairEngagementFlowEvent::kDiscoveryUiShown);
  RecordStructuredDiscoveryNotificationShown(*device,
                                             GetBluetoothDevice(device));
}

void QuickPairMetricsLogger::OnPairingStart(scoped_refptr<Device> device) {
  RecordFastPairInitializePairingProcessEvent(
      *device, FastPairInitializePairingProcessEvent::kInitializationStarted);

  switch (device->protocol()) {
    case Protocol::kFastPairSubsequent:
      RecordSubsequentSuccessFunnelFlow(
          FastPairSubsequentSuccessFunnelEvent::kInitializationStarted);
      break;
    case Protocol::kFastPairInitial:
      RecordInitialSuccessFunnelFlow(
          FastPairInitialSuccessFunnelEvent::kInitializationStarted);
      break;
    case Protocol::kFastPairRetroactive:
      RecordRetroactiveSuccessFunnelFlow(
          FastPairRetroactiveSuccessFunnelEvent::kInitializationStarted);
      break;
  }
}

void QuickPairMetricsLogger::OnHandshakeComplete(scoped_refptr<Device> device) {
  RecordFastPairInitializePairingProcessEvent(
      *device, FastPairInitializePairingProcessEvent::kInitializationComplete);

  switch (device->protocol()) {
    case Protocol::kFastPairSubsequent:
      RecordSubsequentSuccessFunnelFlow(
          FastPairSubsequentSuccessFunnelEvent::kPairingStarted);
      break;
    case Protocol::kFastPairInitial:
      RecordInitialSuccessFunnelFlow(
          FastPairInitialSuccessFunnelEvent::kPairingStarted);
      break;
    case Protocol::kFastPairRetroactive:
      RecordRetroactiveSuccessFunnelFlow(
          FastPairRetroactiveSuccessFunnelEvent::kWritingAccountKey);
      break;
  }
}

void QuickPairMetricsLogger::OnPairingComplete(scoped_refptr<Device> device) {
  switch (device->protocol()) {
    case Protocol::kFastPairSubsequent:
      RecordSubsequentSuccessFunnelFlow(
          FastPairSubsequentSuccessFunnelEvent::kProcessComplete);
      RecordStructuredPairingComplete(*device, GetBluetoothDevice(device));
      break;
    case Protocol::kFastPairInitial:
      RecordInitialSuccessFunnelFlow(
          FastPairInitialSuccessFunnelEvent::kProcessComplete);
      RecordStructuredPairingComplete(*device, GetBluetoothDevice(device));
      break;
    case Protocol::kFastPairRetroactive:
      break;
  }
}

void QuickPairMetricsLogger::OnRetroactivePairFound(
    scoped_refptr<Device> device) {
  // When a device for the retroactive pairing scenario is detected, the
  // corresponding "Associate Account" retroactive pairing notification is
  // presenting to the user.
  AttemptRecordingFastPairRetroactiveEngagementFlow(
      *device,
      FastPairRetroactiveEngagementFlowEvent::kAssociateAccountUiShown);
  GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
      device, FastPairRetroactiveEngagementFlowEvent::kAssociateAccountUiShown);
  RecordRetroactiveSuccessFunnelFlow(
      FastPairRetroactiveSuccessFunnelEvent::kDeviceDetected);
  RecordStructuredPairingStarted(*device, GetBluetoothDevice(device));
}

void QuickPairMetricsLogger::OnAssociateAccountAction(
    scoped_refptr<Device> device,
    AssociateAccountAction action) {
  switch (action) {
    case AssociateAccountAction::kAssociateAccount: {
      if (base::Contains(associate_account_learn_more_devices_, device)) {
        AttemptRecordingFastPairRetroactiveEngagementFlow(
            *device, FastPairRetroactiveEngagementFlowEvent::
                         kAssociateAccountSavePressedAfterLearnMorePressed);
        GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
            device, FastPairRetroactiveEngagementFlowEvent::
                        kAssociateAccountSavePressedAfterLearnMorePressed);
        associate_account_learn_more_devices_.erase(device);
        break;
      }

      PrefService* pref = GetLastActiveUserPrefService();
      if (pref->FindPreference(ash::prefs::kUserPairedWithFastPair)) {
        pref->SetBoolean(ash::prefs::kUserPairedWithFastPair, true);
      }

      AttemptRecordingFastPairRetroactiveEngagementFlow(
          *device,
          FastPairRetroactiveEngagementFlowEvent::kAssociateAccountSavePressed);
      GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
          device,
          FastPairRetroactiveEngagementFlowEvent::kAssociateAccountSavePressed);
      RecordRetroactiveSuccessFunnelFlow(
          FastPairRetroactiveSuccessFunnelEvent::kSaveRequested);
      break;
    }
    case AssociateAccountAction::kLearnMore:
      // We need to record whether or not the Associate Account UI for this
      // device has had the Learn More button pressed because since the
      // Learn More button is not a terminal state, we need to record
      // if the subsequent terminal states were reached after the user
      // has learned more about saving their accounts. So we will check
      // this map when the user dismisses or saves their account in order
      // to capture whether or not the user elected to learn more beforehand.
      associate_account_learn_more_devices_.insert(device);

      AttemptRecordingFastPairRetroactiveEngagementFlow(
          *device, FastPairRetroactiveEngagementFlowEvent::
                       kAssociateAccountLearnMorePressed);
      GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
          device, FastPairRetroactiveEngagementFlowEvent::
                      kAssociateAccountLearnMorePressed);
      break;
    case AssociateAccountAction::kDismissedByUser:
      if (base::Contains(associate_account_learn_more_devices_, device)) {
        AttemptRecordingFastPairRetroactiveEngagementFlow(
            *device, FastPairRetroactiveEngagementFlowEvent::
                         kAssociateAccountDismissedByUserAfterLearnMorePressed);
        GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
            device, FastPairRetroactiveEngagementFlowEvent::
                        kAssociateAccountDismissedByUserAfterLearnMorePressed);
        associate_account_learn_more_devices_.erase(device);
        break;
      }

      AttemptRecordingFastPairRetroactiveEngagementFlow(
          *device, FastPairRetroactiveEngagementFlowEvent::
                       kAssociateAccountUiDismissedByUser);
      GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
          device, FastPairRetroactiveEngagementFlowEvent::
                      kAssociateAccountUiDismissedByUser);
      break;
    case AssociateAccountAction::kDismissedByTimeout:
      if (base::Contains(associate_account_learn_more_devices_, device)) {
        AttemptRecordingFastPairRetroactiveEngagementFlow(
            *device,
            FastPairRetroactiveEngagementFlowEvent::
                kAssociateAccountDismissedByTimeoutAfterLearnMorePressed);
        GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
            device,
            FastPairRetroactiveEngagementFlowEvent::
                kAssociateAccountDismissedByTimeoutAfterLearnMorePressed);
        associate_account_learn_more_devices_.erase(device);
        break;
      }

      AttemptRecordingFastPairRetroactiveEngagementFlow(
          *device, FastPairRetroactiveEngagementFlowEvent::
                       kAssociateAccountUiDismissedByTimeout);
      GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
          device, FastPairRetroactiveEngagementFlowEvent::
                      kAssociateAccountUiDismissedByTimeout);
      break;
    case AssociateAccountAction::kDismissedByOs:
      if (base::Contains(associate_account_learn_more_devices_, device)) {
        AttemptRecordingFastPairRetroactiveEngagementFlow(
            *device, FastPairRetroactiveEngagementFlowEvent::
                         kAssociateAccountDismissedAfterLearnMorePressed);
        GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
            device, FastPairRetroactiveEngagementFlowEvent::
                        kAssociateAccountDismissedAfterLearnMorePressed);
        associate_account_learn_more_devices_.erase(device);
        break;
      }

      AttemptRecordingFastPairRetroactiveEngagementFlow(
          *device,
          FastPairRetroactiveEngagementFlowEvent::kAssociateAccountUiDismissed);
      GetDeviceMetadataAndLogRetroactiveEngagementFunnelWithMetadata(
          device,
          FastPairRetroactiveEngagementFlowEvent::kAssociateAccountUiDismissed);
      break;
  }
}

void QuickPairMetricsLogger::OnAccountKeyWrite(
    scoped_refptr<Device> device,
    std::optional<AccountKeyFailure> error) {
  switch (device->protocol()) {
    case Protocol::kFastPairSubsequent:
      // TODO(b/259443372): Record this case once we implement account key
      // writing in all scenarios,
      NOTREACHED();
    case Protocol::kFastPairInitial:
      break;
    case Protocol::kFastPairRetroactive:
      RecordRetroactivePairingResult(/*success=*/!error.has_value());

      if (!error.has_value()) {
        RecordRetroactiveSuccessFunnelFlow(
            FastPairRetroactiveSuccessFunnelEvent::kAccountKeyWrittenToDevice);
        RecordStructuredPairingComplete(*device, GetBluetoothDevice(device));
      }
      // TODO(jackshira): Log new PairFailure case here.
      break;
  }

  if (error.has_value()) {
    RecordAccountKeyResult(*device, /*success=*/false);
    RecordAccountKeyFailureReason(*device, error.value());
    feature_usage_metrics_logger_->RecordUsage(/*success=*/false);
    return;
  }

  RecordAccountKeyResult(*device, /*success=*/true);
  feature_usage_metrics_logger_->RecordUsage(/*success=*/true);
}

void QuickPairMetricsLogger::OnCompanionAppAction(scoped_refptr<Device> device,
                                                  CompanionAppAction action) {}

void QuickPairMetricsLogger::OnDeviceLost(scoped_refptr<Device> device) {}

}  // namespace quick_pair
}  // namespace ash