UTEventReporting.sys.mjs (1374B)
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 * Note: the schema can be found in 7 * https://searchfox.org/mozilla-central/source/toolkit/components/telemetry/Events.yaml 8 */ 9 const EXTRAS_FIELD_NAMES = [ 10 "addon_version", 11 "session_id", 12 "page", 13 "user_prefs", 14 "action_position", 15 ]; 16 17 export class UTEventReporting { 18 constructor() { 19 this.sendUserEvent = this.sendUserEvent.bind(this); 20 this.sendSessionEndEvent = this.sendSessionEndEvent.bind(this); 21 } 22 23 _createExtras(data, value) { 24 // Make a copy of the given data and delete/modify it as needed. 25 let utExtras = Object.assign({}, data); 26 for (let field of Object.keys(utExtras)) { 27 if (!EXTRAS_FIELD_NAMES.includes(field)) { 28 delete utExtras[field]; 29 } 30 } 31 utExtras.value = value; 32 return utExtras; 33 } 34 35 sendUserEvent(data) { 36 const eventName = data.event 37 .split("_") 38 .map(word => word[0] + word.slice(1).toLowerCase()) 39 .join(""); 40 Glean.activityStream[`event${eventName}`].record( 41 this._createExtras(data, data.source) 42 ); 43 } 44 45 sendSessionEndEvent(data) { 46 Glean.activityStream.endSession.record( 47 this._createExtras(data, data.session_duration) 48 ); 49 } 50 }