You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnalyticsAdapter.js 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import {
  2. TYPE_OPERATIONAL,
  3. TYPE_PAGE,
  4. TYPE_TRACK,
  5. TYPE_UI
  6. } from '../../service/statistics/AnalyticsEvents';
  7. import { getLogger } from 'jitsi-meet-logger';
  8. import RTCBrowserType from '../RTC/RTCBrowserType';
  9. import Settings from '../settings/Settings';
  10. const MAX_CACHE_SIZE = 100;
  11. // eslist-disable-line no-undef
  12. const logger = getLogger(__filename);
  13. /**
  14. * This class provides an API to lib-jitsi-meet and its users for sending
  15. * analytics events. It serves as a bridge to different backend implementations
  16. * ("analytics handlers") and a cache for events attempted to be sent before
  17. * the analytics handlers were enabled.
  18. *
  19. * The API is designed to be an easy replacement for the previous version of
  20. * this adapter, and is meant to be extended with more convenience methods.
  21. *
  22. *
  23. * The API calls are translated to objects with the following structure, which
  24. * are then passed to the sendEvent(event) function of the underlying handlers:
  25. *
  26. * {
  27. * type,
  28. *
  29. * action,
  30. * actionSubject,
  31. * actionSubjectId,
  32. * attributes,
  33. * categories,
  34. * containerId,
  35. * containerType,
  36. * name,
  37. * objectId,
  38. * objectType,
  39. * source,
  40. * tags
  41. * }
  42. *
  43. * The 'type' is one of 'operational', 'page', 'track' or 'ui', and some of the
  44. * other properties are considered required according to the type.
  45. *
  46. * For events with type 'page', the required properties are: name.
  47. *
  48. * For events with type 'operational' and 'ui', the required properties are:
  49. * action, actionSubject, source
  50. *
  51. * For events with type 'page', the required properties are:
  52. * action, actionSubject, source, containerType, containerId, objectType,
  53. * objectId
  54. */
  55. class AnalyticsAdapter {
  56. /**
  57. * Creates new AnalyticsAdapter instance.
  58. */
  59. constructor() {
  60. /**
  61. * Whether this AnalyticsAdapter has been disposed of or not. Once this
  62. * is set to true, the AnalyticsAdapter is disabled and does not accept
  63. * any more events, and it can not be re-enabled.
  64. * @type {boolean}
  65. */
  66. this.disposed = false;
  67. /**
  68. * The set of handlers to which events will be sent.
  69. * @type {Set<any>}
  70. */
  71. this.analyticsHandlers = new Set();
  72. /**
  73. * The cache of events which are not sent yet. The cache is enabled
  74. * while this field is truthy, and disabled otherwise.
  75. * @type {Array}
  76. */
  77. this.cache = [];
  78. /**
  79. * Map of properties that will be added to every event. Note that the
  80. * keys will be prefixed with "permanent.".
  81. */
  82. this.permanentProperties = {};
  83. /**
  84. * The name of the conference that this AnalyticsAdapter is associated
  85. * with.
  86. * @type {null}
  87. */
  88. this.conferenceName = '';
  89. this.addPermanentProperties({
  90. 'callstats_name': Settings.callStatsUserName,
  91. 'user_agent': navigator.userAgent,
  92. 'browser_name': RTCBrowserType.getBrowserName()
  93. });
  94. }
  95. /**
  96. * Dispose analytics. Clears all handlers.
  97. */
  98. dispose() {
  99. this.setAnalyticsHandlers([]);
  100. this.disposed = true;
  101. }
  102. /**
  103. * Sets the handlers that are going to be used to send analytics. Sends any
  104. * cached events.
  105. * @param {Array} handlers the handlers
  106. */
  107. setAnalyticsHandlers(handlers) {
  108. if (this.disposed) {
  109. return;
  110. }
  111. this.analyticsHandlers = new Set(handlers);
  112. // Note that we disable the cache even if the set of handlers is empty.
  113. const cache = this.cache;
  114. this.cache = null;
  115. if (cache) {
  116. cache.forEach(event => this._sendEvent(event));
  117. }
  118. }
  119. /**
  120. * Adds a set of permanent properties to this this AnalyticsAdapter.
  121. * Permanent properties will be added as "attributes" to events sent to
  122. * the underlying "analytics handlers", and their keys will be prefixed
  123. * by "permanent_", i.e. adding a permanent property {key: "value"} will
  124. * result in {"permanent_key": "value"} object to be added to the
  125. * "attributes" field of events.
  126. *
  127. * @param {Object} properties the properties to add
  128. */
  129. addPermanentProperties(properties) {
  130. for (const property in properties) {
  131. if (properties.hasOwnProperty(property)) {
  132. this.permanentProperties[`permanent_${property}`]
  133. = properties[property];
  134. }
  135. }
  136. }
  137. /**
  138. * Sets the name of the conference that this AnalyticsAdapter is associated
  139. * with.
  140. * @param name the name to set.
  141. */
  142. setConferenceName(name) {
  143. this.conferenceName = name;
  144. this.addPermanentProperties({ 'conference_name': name });
  145. }
  146. /**
  147. * Sends an event with a given name and given properties. The first
  148. * parameter is either a string or an object. If it is a string, it is used
  149. * as the event name and the second parameter is used at the attributes to
  150. * attach to the event. If it is an object, it represents the whole event,
  151. * including any desired attributes, and the second parameter is ignored.
  152. *
  153. * @param {String|Object} eventName either a string to be used as the name
  154. * of the event, or an event object. If an event object is passed, the
  155. * properties parameters is ignored.
  156. * @param {Object} properties the properties/attributes to attach to the
  157. * event, if eventName is a string.
  158. */
  159. sendEvent(eventName, properties = {}) {
  160. let event = null;
  161. if (typeof eventName === 'string') {
  162. event = {
  163. type: TYPE_OPERATIONAL,
  164. action: eventName,
  165. actionSubject: eventName,
  166. source: eventName,
  167. attributes: properties
  168. };
  169. } else if (typeof eventName === 'object') {
  170. event = eventName;
  171. }
  172. if (!this._verifyRequiredFields(event)) {
  173. logger.error(
  174. `Dropping a mis-formatted event: ${JSON.stringify(event)}`);
  175. return;
  176. }
  177. this._sendEvent(event);
  178. }
  179. /**
  180. * Checks whether an event has all of the required fields set, and tries
  181. * to fill in some of the missing fields with reasonable default values.
  182. * Returns true if after this operation the event has all of the required
  183. * fields set, and false otherwise (if some of the required fields were not
  184. * set and the attempt to fill them in with a default failed).
  185. *
  186. * @param event the event object.
  187. * @return {boolean} true if the event (after the call to this function)
  188. * contains all of the required fields, and false otherwise.
  189. * @private
  190. */
  191. _verifyRequiredFields(event) {
  192. if (!event) {
  193. return false;
  194. }
  195. if (!event.type) {
  196. event.type = TYPE_OPERATIONAL;
  197. }
  198. const type = event.type;
  199. if (type !== TYPE_OPERATIONAL && type !== TYPE_PAGE
  200. && type !== TYPE_UI && type !== TYPE_TRACK) {
  201. logger.error(`Unknown event type: ${type}`);
  202. return false;
  203. }
  204. if (type === TYPE_PAGE) {
  205. return Boolean(event.name);
  206. }
  207. // Try to set some reasonable default values in case some of the
  208. // parameters required by the handler API are missing.
  209. event.action = event.action || event.name || event.actionSubject;
  210. event.actionSubject = event.actionSubject || event.name || event.action;
  211. event.source = event.source || event.name || event.action
  212. || event.actionSubject;
  213. if (!event.action || !event.actionSubject || !event.source) {
  214. logger.error(
  215. 'Required field missing (action, actionSubject or source)');
  216. return false;
  217. }
  218. // Track events have additional required fields.
  219. if (type === TYPE_TRACK) {
  220. event.objectType = event.objectType || 'generic-object-type';
  221. event.containerType = event.containerType || 'conference';
  222. if (event.containerType === 'conference' && !event.containerId) {
  223. event.containerId = this.conferenceName;
  224. }
  225. if (!event.objectType || !event.objectId
  226. || !event.containerType || !event.containerId) {
  227. logger.error(
  228. 'Required field missing (containerId, containerType, '
  229. + 'objectId or objectType)');
  230. return false;
  231. }
  232. }
  233. return true;
  234. }
  235. /**
  236. * Saves an event to the cache, if the cache is enabled.
  237. * @param event the event to save.
  238. * @returns {boolean} true if the event was saved, and false otherwise (i.e.
  239. * if the cache was disabled).
  240. * @private
  241. */
  242. _maybeCacheEvent(event) {
  243. if (this.cache) {
  244. this.cache.push(event);
  245. // We limit the size of the cache, in case the user fails to ever
  246. // set the analytics handlers.
  247. if (this.cache.length > MAX_CACHE_SIZE) {
  248. this.cache.splice(0, 1);
  249. }
  250. return true;
  251. }
  252. return false;
  253. }
  254. /**
  255. *
  256. * @param event
  257. * @private
  258. */
  259. _sendEvent(event) {
  260. if (this._maybeCacheEvent(event)) {
  261. // The event was consumed by the cache.
  262. } else {
  263. // We append the permanent properties at the time we send the event,
  264. // not at the time we receive it.
  265. this._appendPermanentProperties(event);
  266. this.analyticsHandlers.forEach(handler => handler.sendEvent(event));
  267. }
  268. }
  269. /**
  270. * Extends an event object with the configured permanent properties.
  271. * @param event the event to extend with permanent properties.
  272. * @private
  273. */
  274. _appendPermanentProperties(event) {
  275. if (!event.attributes) {
  276. event.attributes = {};
  277. }
  278. event.attributes
  279. = Object.assign(event.attributes, this.permanentProperties);
  280. }
  281. }
  282. export default new AnalyticsAdapter();