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.

AbstractHandler.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. export interface IEvent {
  2. action?: string;
  3. actionSubject?: string;
  4. attributes?: {
  5. [key: string]: string | undefined;
  6. };
  7. name?: string;
  8. source?: string;
  9. type?: string;
  10. }
  11. interface IOptions {
  12. amplitudeAPPKey?: string;
  13. blackListedEvents?: string[];
  14. envType?: string;
  15. googleAnalyticsTrackingId?: string;
  16. group?: string;
  17. host?: string;
  18. matomoEndpoint?: string;
  19. matomoSiteID?: string;
  20. product?: string;
  21. subproduct?: string;
  22. user?: string;
  23. version?: string;
  24. whiteListedEvents?: string[];
  25. }
  26. /**
  27. * Abstract implementation of analytics handler.
  28. */
  29. export default class AbstractHandler {
  30. _enabled: boolean;
  31. _whiteListedEvents: Array<string> | undefined;
  32. _blackListedEvents: Array<string> | undefined;
  33. /**
  34. * Creates new instance.
  35. *
  36. * @param {Object} options - Optional parameters.
  37. */
  38. constructor(options: IOptions = {}) {
  39. this._enabled = false;
  40. this._whiteListedEvents = options.whiteListedEvents;
  41. // FIXME:
  42. // Keeping the list with the very noisy events so that we don't flood with events whoever hasn't configured
  43. // white/black lists yet. We need to solve this issue properly by either making these events not so noisy or
  44. // by removing them completely from the code.
  45. this._blackListedEvents = [
  46. ...(options.blackListedEvents || []), // eslint-disable-line no-extra-parens
  47. 'e2e_rtt', 'rtp.stats', 'rtt.by.region', 'available.device', 'stream.switch.delay', 'ice.state.changed',
  48. 'ice.duration', 'peer.conn.status.duration'
  49. ];
  50. }
  51. /**
  52. * Extracts a name for the event from the event properties.
  53. *
  54. * @param {Object} event - The analytics event.
  55. * @returns {string} - The extracted name.
  56. */
  57. _extractName(event: IEvent) {
  58. // Page events have a single 'name' field.
  59. if (event.type === 'page') {
  60. return event.name;
  61. }
  62. const {
  63. action,
  64. actionSubject,
  65. source
  66. } = event;
  67. // All events have action, actionSubject, and source fields. All
  68. // three fields are required, and often jitsi-meet and
  69. // lib-jitsi-meet use the same value when separate values are not
  70. // necessary (i.e. event.action == event.actionSubject).
  71. // Here we concatenate these three fields, but avoid adding the same
  72. // value twice, because it would only make the event's name harder
  73. // to read.
  74. let name = action;
  75. if (actionSubject && actionSubject !== action) {
  76. name = `${actionSubject}.${action}`;
  77. }
  78. if (source && source !== action) {
  79. name = `${source}.${name}`;
  80. }
  81. return name;
  82. }
  83. /**
  84. * Checks if an event should be ignored or not.
  85. *
  86. * @param {Object} event - The event.
  87. * @returns {boolean}
  88. */
  89. _shouldIgnore(event: IEvent) {
  90. if (!event || !this._enabled) {
  91. return true;
  92. }
  93. const name = this._extractName(event) ?? '';
  94. if (Array.isArray(this._whiteListedEvents)) {
  95. return this._whiteListedEvents.indexOf(name) === -1;
  96. }
  97. if (Array.isArray(this._blackListedEvents)) {
  98. return this._blackListedEvents.indexOf(name) !== -1;
  99. }
  100. return false;
  101. }
  102. }