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"use strict"; 6 7const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); 8 9/** 10 * Note: the schema can be found in 11 * https://searchfox.org/mozilla-central/source/toolkit/components/telemetry/Events.yaml 12 */ 13const EXTRAS_FIELD_NAMES = [ 14 "addon_version", 15 "session_id", 16 "page", 17 "user_prefs", 18 "action_position", 19]; 20 21this.UTEventReporting = class UTEventReporting { 22 constructor() { 23 Services.telemetry.setEventRecordingEnabled("activity_stream", true); 24 this.sendUserEvent = this.sendUserEvent.bind(this); 25 this.sendSessionEndEvent = this.sendSessionEndEvent.bind(this); 26 } 27 28 _createExtras(data) { 29 // Make a copy of the given data and delete/modify it as needed. 30 let utExtras = Object.assign({}, data); 31 for (let field of Object.keys(utExtras)) { 32 if (EXTRAS_FIELD_NAMES.includes(field)) { 33 utExtras[field] = String(utExtras[field]); 34 continue; 35 } 36 delete utExtras[field]; 37 } 38 return utExtras; 39 } 40 41 sendUserEvent(data) { 42 let mainFields = ["event", "source"]; 43 let eventFields = mainFields.map(field => String(data[field]) || null); 44 45 Services.telemetry.recordEvent( 46 "activity_stream", 47 "event", 48 ...eventFields, 49 this._createExtras(data) 50 ); 51 } 52 53 sendSessionEndEvent(data) { 54 Services.telemetry.recordEvent( 55 "activity_stream", 56 "end", 57 "session", 58 String(data.session_duration), 59 this._createExtras(data) 60 ); 61 } 62 63 uninit() { 64 Services.telemetry.setEventRecordingEnabled("activity_stream", false); 65 } 66}; 67 68const EXPORTED_SYMBOLS = ["UTEventReporting"]; 69