1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // FxA Telemetry support. For hysterical raisins, the actual implementation
6 // is inside "sync". We should move the core implementation somewhere that's
7 // sanely shared (eg, services-common?), but let's wait and see where we end up
10 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
14 ChromeUtils.defineESModuleGetters(lazy, {
15 CryptoUtils: "resource://services-crypto/utils.sys.mjs",
17 // We use this observers module because we leverage its support for richer
19 Observers: "resource://services-common/observers.sys.mjs",
25 } from "resource://gre/modules/FxAccountsCommon.sys.mjs";
27 const PREF_SANITIZED_UID = PREF_ACCOUNT_ROOT + "telemetry.sanitized_uid";
28 XPCOMUtils.defineLazyPreferenceGetter(
35 export class FxAccountsTelemetry {
38 Services.telemetry.setEventRecordingEnabled("fxa", true);
41 // Records an event *in the Fxa/Sync ping*.
42 recordEvent(object, method, value, extra = undefined) {
43 // We need to ensure the telemetry module is loaded.
44 ChromeUtils.importESModule("resource://services-sync/telemetry.sys.mjs");
45 // Now it will be listening for the notifications...
46 lazy.Observers.notify("fxa:telemetry:event", {
55 return Services.uuid.generateUUID().toString().slice(1, -1);
58 // A flow ID can be anything that's "probably" unique, so for now use a UUID.
60 return this.generateUUID();
63 // FxA- and Sync-related metrics are submitted in a special-purpose "sync ping". This ping
64 // identifies the user by a version of their FxA uid that is HMAC-ed with a server-side secret
65 // key, in an attempt to provide a bit of anonymity.
67 // Secret back-channel by which tokenserver client code can set the hashed UID.
68 // This value conceptually belongs to FxA, but we currently get it from tokenserver,
69 // so there's some light hackery to put it in the right place.
70 _setHashedUID(hashedUID) {
72 Services.prefs.clearUserPref(PREF_SANITIZED_UID);
74 Services.prefs.setStringPref(PREF_SANITIZED_UID, hashedUID);
79 // Sadly, we can only currently obtain this value if the user has enabled sync.
80 return lazy.pref_sanitizedUid || null;
83 // Sanitize the ID of a device into something suitable for including in the
84 // ping. Returns null if no transformation is possible.
85 sanitizeDeviceId(deviceId) {
86 const uid = this.getSanitizedUID();
88 // Sadly, we can only currently get this if the user has enabled sync.
91 // Combine the raw device id with the sanitized uid to create a stable
92 // unique identifier that can't be mapped back to the user's FxA
93 // identity without knowing the metrics HMAC key.
94 // The result is 64 bytes long, which in retrospect is probably excessive,
95 // but it's already shipping...
96 return lazy.CryptoUtils.sha256(deviceId + uid);
99 // Record the connection of FxA or one of its services.
100 // Note that you must call this before performing the actual connection
101 // or we may record incorrect data - for example, we will not be able to
102 // determine whether FxA itself was connected before this call.
104 // Currently sends an event in the main telemetry event ping rather than the
105 // FxA/Sync ping (although this might change in the future)
107 // @param services - An array of service names which should be recorded. FxA
108 // itself is not counted as a "service" - ie, an empty array should be passed
109 // if the account is connected without anything else .
111 // @param how - How the connection was done.
112 async recordConnection(services, how = null) {
115 // Record that fxa was connected if it isn't currently - it will be soon.
116 if (!(await this._fxai.getUserAccountData())) {
119 // Events.yaml only declares "sync" as a valid service.
120 if (services.includes("sync")) {
123 Services.telemetry.recordEvent("fxa", "connect", "account", how, extra);
125 log.error("Failed to record connection telemetry", ex);
126 console.error("Failed to record connection telemetry", ex);
130 // Record the disconnection of FxA or one of its services.
131 // Note that you must call this before performing the actual disconnection
132 // or we may record incomplete data - for example, if this is called after
133 // disconnection, we've almost certainly lost the ability to record what
134 // services were enabled prior to disconnection.
136 // Currently sends an event in the main telemetry event ping rather than the
137 // FxA/Sync ping (although this might change in the future)
139 // @param service - the service being disconnected. If null, the account
140 // itself is being disconnected, so all connected services are too.
142 // @param how - how the disconnection was done.
143 async recordDisconnection(service = null, how = null) {
148 // We need a way to enumerate all services - but for now we just hard-code
149 // all possibilities here.
150 if (Services.prefs.prefHasUserValue("services.sync.username")) {
153 } else if (service == "sync") {
154 extra[service] = "true";
156 // Events.yaml only declares "sync" as a valid service.
158 `recordDisconnection has invalid value for service: ${service}`
161 Services.telemetry.recordEvent(
169 log.error("Failed to record disconnection telemetry", ex);
170 console.error("Failed to record disconnection telemetry", ex);