tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

telemetry.js (1968B)


      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
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 /**
      6 * Usage:
      7 *
      8 * import { recordEvent } from "src/utils/telemetry";
      9 *
     10 * // Event without extra properties
     11 * recordEvent("add_breakpoint");
     12 *
     13 * // Event with extra properties
     14 * recordEvent("pause", {
     15 *   "reason": "debugger-statement",
     16 *   "collapsed_callstacks": 1
     17 * });
     18 *
     19 * // If the properties are in multiple code paths and you can't send them all
     20 * // in one go you will need to use the full telemetry API.
     21 *
     22 * const Telemetry = require("devtools/client/shared/telemetry");
     23 *
     24 * const telemetry = new Telemetry();
     25 *
     26 * // Prepare the event and define which properties to expect.
     27 * //
     28 * // NOTE: You CAN send properties before preparing the event.
     29 * //
     30 *  telemetry.preparePendingEvent(this, "pause", "debugger", null, [
     31 *   "reason", "collapsed_callstacks"
     32 * ]);
     33 *
     34 * // Elsewhere in another codepath send the reason property
     35 * telemetry.addEventProperty(
     36 *   this, "pause", "debugger", null, "reason", "debugger-statement"
     37 * );
     38 *
     39 * // Elsewhere in another codepath send the collapsed_callstacks property
     40 * telemetry.addEventProperty(
     41 *   this, "pause", "debugger", null, "collapsed_callstacks", 1
     42 * );
     43 */
     44 
     45 import { isNode } from "./environment";
     46 
     47 let telemetry;
     48 
     49 if (isNode()) {
     50  const Telemetry = require("resource://devtools/client/shared/telemetry.js");
     51  telemetry = new Telemetry();
     52 }
     53 
     54 export function setToolboxTelemetry(toolboxTelemetry) {
     55  telemetry = toolboxTelemetry;
     56 }
     57 
     58 /**
     59 * @memberof utils/telemetry
     60 * @static
     61 */
     62 export function recordEvent(eventName, fields = {}) {
     63  telemetry.recordEvent(eventName, "debugger", null, fields);
     64 
     65  if (isNode()) {
     66    const { events } = window.dbg._telemetry;
     67    if (!events[eventName]) {
     68      events[eventName] = [];
     69    }
     70    events[eventName].push(fields);
     71  }
     72 }