summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/telemetry.js
blob: 0e0c8766bbf21f6d0f74a4a97be7321ae7dc9c05 (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
/* 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/>. */

/**
 * Usage:
 *
 * import { recordEvent } from "src/utils/telemetry";
 *
 * // Event without extra properties
 * recordEvent("add_breakpoint");
 *
 * // Event with extra properties
 * recordEvent("pause", {
 *   "reason": "debugger-statement",
 *   "collapsed_callstacks": 1
 * });
 *
 * // If the properties are in multiple code paths and you can't send them all
 * // in one go you will need to use the full telemetry API.
 *
 * const Telemetry = require("devtools/client/shared/telemetry");
 *
 * const telemetry = new Telemetry();
 *
 * // Prepare the event and define which properties to expect.
 * //
 * // NOTE: You CAN send properties before preparing the event.
 * //
 *  telemetry.preparePendingEvent(this, "pause", "debugger", null, [
 *   "reason", "collapsed_callstacks"
 * ]);
 *
 * // Elsewhere in another codepath send the reason property
 * telemetry.addEventProperty(
 *   this, "pause", "debugger", null, "reason", "debugger-statement"
 * );
 *
 * // Elsewhere in another codepath send the collapsed_callstacks property
 * telemetry.addEventProperty(
 *   this, "pause", "debugger", null, "collapsed_callstacks", 1
 * );
 */

import { isNode } from "./environment";

let telemetry;

if (isNode()) {
  const Telemetry = require("resource://devtools/client/shared/telemetry.js");
  telemetry = new Telemetry();
}

export function setToolboxTelemetry(toolboxTelemetry) {
  telemetry = toolboxTelemetry;
}

/**
 * @memberof utils/telemetry
 * @static
 */
export function recordEvent(eventName, fields = {}) {
  telemetry.recordEvent(eventName, "debugger", null, fields);

  if (isNode()) {
    const { events } = window.dbg._telemetry;
    if (!events[eventName]) {
      events[eventName] = [];
    }
    events[eventName].push(fields);
  }
}