summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutwelcome/modules/AboutWelcomeTelemetry.sys.mjs
blob: 1447d3ebde8f3d2664ba53c74c7b55c8be97cd53 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  AttributionCode: "resource:///modules/AttributionCode.sys.mjs",
  ClientID: "resource://gre/modules/ClientID.sys.mjs",
  TelemetrySession: "resource://gre/modules/TelemetrySession.sys.mjs",
});

ChromeUtils.defineLazyGetter(lazy, "telemetryClientId", () =>
  lazy.ClientID.getClientID()
);
ChromeUtils.defineLazyGetter(
  lazy,
  "browserSessionId",
  () => lazy.TelemetrySession.getMetadata("").sessionId
);

ChromeUtils.defineLazyGetter(lazy, "log", () => {
  const { Logger } = ChromeUtils.importESModule(
    "resource://messaging-system/lib/Logger.sys.mjs"
  );
  return new Logger("AboutWelcomeTelemetry");
});

export class AboutWelcomeTelemetry {
  constructor() {
    XPCOMUtils.defineLazyPreferenceGetter(
      this,
      "telemetryEnabled",
      "browser.newtabpage.activity-stream.telemetry",
      false
    );
  }

  /**
   * Attach browser attribution data to a ping payload.
   *
   * It intentionally queries the *cached* attribution data other than calling
   * `getAttrDataAsync()` in order to minimize the overhead here.
   * For the same reason, we are not querying the attribution data from
   * `TelemetryEnvironment.currentEnvironment.settings`.
   *
   * In practice, it's very likely that the attribution data is already read
   * and cached at some point by `AboutWelcomeParent`, so it should be able to
   * read the cached results for the most if not all of the pings.
   */
  _maybeAttachAttribution(ping) {
    const attribution = lazy.AttributionCode.getCachedAttributionData();
    if (attribution && Object.keys(attribution).length) {
      ping.attribution = attribution;
    }
    return ping;
  }

  async _createPing(event) {
    if (event.event_context && typeof event.event_context === "object") {
      event.event_context = JSON.stringify(event.event_context);
    }
    let ping = {
      ...event,
      addon_version: Services.appinfo.appBuildID,
      locale: Services.locale.appLocaleAsBCP47,
      client_id: await lazy.telemetryClientId,
      browser_session_id: lazy.browserSessionId,
    };

    return this._maybeAttachAttribution(ping);
  }

  /**
   * Augment the provided event with some metadata and then send it
   * to the messaging-system's onboarding endpoint.
   *
   * Is sometimes used by non-onboarding events.
   *
   * @param event - an object almost certainly from an onboarding flow (though
   *                there is a case where spotlight may use this, too)
   *                containing a nested structure of data for reporting as
   *                telemetry, as documented in
   * https://firefox-source-docs.mozilla.org/browser/components/newtab/docs/v2-system-addon/data_events.html
   *                Does not have all of its data (`_createPing` will augment
   *                with ids and attribution if available).
   */
  async sendTelemetry(event) {
    if (!this.telemetryEnabled) {
      return;
    }

    const ping = await this._createPing(event);

    try {
      this.submitGleanPingForPing(ping);
    } catch (e) {
      // Though Glean APIs are forbidden to throw, it may be possible that a
      // mismatch between the shape of `ping` and the defined metrics is not
      // adequately handled.
      Glean.messagingSystem.gleanPingForPingFailures.add(1);
    }
  }

  /**
   * Tries to infer appropriate Glean metrics on the "messaging-system" ping,
   * sets them, and submits a "messaging-system" ping.
   *
   * Does not check if telemetry is enabled.
   * (Though Glean will check the global prefs).
   *
   * Note: This is a very unusual use of Glean that is specific to the use-
   *       cases of Messaging System. Please do not copy this pattern.
   */
  submitGleanPingForPing(ping) {
    lazy.log.debug(`Submitting Glean ping for ${JSON.stringify(ping)}`);
    // event.event_context is an object, but it may have been stringified.
    let event_context = ping?.event_context;
    let shopping_callout_impression =
      ping?.message_id?.startsWith("FAKESPOT_CALLOUT") &&
      ping?.event === "IMPRESSION";

    if (typeof event_context === "string") {
      try {
        event_context = JSON.parse(event_context);
        // This code is for directing Shopping component based clicks into
        // the Glean Events ping.
        if (
          event_context?.page === "about:shoppingsidebar" ||
          shopping_callout_impression
        ) {
          this.handleShoppingPings(ping, event_context);
        }
      } catch (e) {
        // The Empty JSON strings and non-objects often provided by the
        // existing telemetry we need to send failing to parse do not fit in
        // the spirit of what this error is meant to capture. Instead, we want
        // to capture when what we got should have been an object,
        // but failed to parse.
        if (event_context.length && event_context.includes("{")) {
          Glean.messagingSystem.eventContextParseError.add(1);
        }
      }
    }

    // We echo certain properties from event_context into their own metrics
    // to aid analysis.
    if (event_context?.reason) {
      Glean.messagingSystem.eventReason.set(event_context.reason);
    }
    if (event_context?.page) {
      Glean.messagingSystem.eventPage.set(event_context.page);
    }
    if (event_context?.source) {
      Glean.messagingSystem.eventSource.set(event_context.source);
    }
    if (event_context?.screen_family) {
      Glean.messagingSystem.eventScreenFamily.set(event_context.screen_family);
    }
    // Screen_index was being coerced into a boolean value
    // which resulted in 0 (first screen index) being ignored.
    if (Number.isInteger(event_context?.screen_index)) {
      Glean.messagingSystem.eventScreenIndex.set(event_context.screen_index);
    }
    if (event_context?.screen_id) {
      Glean.messagingSystem.eventScreenId.set(event_context.screen_id);
    }
    if (event_context?.screen_initials) {
      Glean.messagingSystem.eventScreenInitials.set(
        event_context.screen_initials
      );
    }

    // The event_context is also provided as-is as stringified JSON.
    if (event_context) {
      Glean.messagingSystem.eventContext.set(JSON.stringify(event_context));
    }

    if ("attribution" in ping) {
      for (const [key, value] of Object.entries(ping.attribution)) {
        const camelKey = this._snakeToCamelCase(key);
        try {
          Glean.messagingSystemAttribution[camelKey].set(value);
        } catch (e) {
          // We here acknowledge that we don't know the full breadth of data
          // being collected. Ideally AttributionCode will later centralize
          // definition and reporting of attribution data and we can be rid of
          // this fail-safe for collecting the names of unknown keys.
          Glean.messagingSystemAttribution.unknownKeys[camelKey].add(1);
        }
      }
    }

    // List of keys handled above.
    const handledKeys = ["event_context", "attribution"];

    for (const [key, value] of Object.entries(ping)) {
      if (handledKeys.includes(key)) {
        continue;
      }
      const camelKey = this._snakeToCamelCase(key);
      try {
        // We here acknowledge that even known keys might have non-scalar
        // values. We're pretty sure we handled them all with handledKeys,
        // but we might not have.
        // Ideally this can later be removed after running for a version or two
        // with no values seen in messaging_system.invalid_nested_data
        if (typeof value === "object") {
          Glean.messagingSystem.invalidNestedData[camelKey].add(1);
        } else {
          Glean.messagingSystem[camelKey].set(value);
        }
      } catch (e) {
        // We here acknowledge that we don't know the full breadth of data being
        // collected. Ideally we will later gain that confidence and can remove
        // this fail-safe for collecting the names of unknown keys.
        Glean.messagingSystem.unknownKeys[camelKey].add(1);
        // TODO(bug 1600008): For testing, also record the overall count.
        Glean.messagingSystem.unknownKeyCount.add(1);
      }
    }

    // With all the metrics set, now it's time to submit this ping.
    GleanPings.messagingSystem.submit();
  }

  _snakeToCamelCase(s) {
    return s.toString().replace(/_([a-z])/gi, (_str, group) => {
      return group.toUpperCase();
    });
  }

  handleShoppingPings(ping, event_context) {
    const message_id = ping?.message_id;
    // This function helps direct a shopping ping to the correct Glean event.
    if (message_id.startsWith("FAKESPOT_OPTIN_DEFAULT")) {
      // Onboarding page message IDs are generated, but can reliably be
      // assumed to start in this manner.
      switch (ping?.event) {
        case "CLICK_BUTTON":
          switch (event_context?.source) {
            case "privacy_policy":
              Glean.shopping.surfaceShowPrivacyPolicyClicked.record();
              break;
            case "terms_of_use":
              Glean.shopping.surfaceShowTermsClicked.record();
              break;
            case "primary_button":
              // corresponds to 'Analyze Reviews'
              Glean.shopping.surfaceOptInClicked.record();
              break;
            case "additional_button":
              // corresponds to "Not Now"
              Glean.shopping.surfaceNotNowClicked.record();
              break;
            case "learn_more":
              Glean.shopping.surfaceLearnMoreClicked.record();
              break;
          }
          break;
        case "IMPRESSION":
          Glean.shopping.surfaceOnboardingDisplayed.record({
            configuration: ping?.message_id,
          });
          break;
      }
    }
    if (message_id.startsWith("FAKESPOT_CALLOUT")) {
      Glean.shopping.addressBarFeatureCalloutDisplayed.record({
        configuration: message_id,
      });
    }
  }
}