modified lib-jitsi-meet dev repo
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AnalyticsAdapter.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Interface for analytics handlers.
  3. */
  4. class AnalyticsAbstract {
  5. sendEvent() {} // eslint-disable-line no-empty-function
  6. }
  7. /**
  8. * Handler that caches all the events.
  9. * @extends AnalyticsAbstract
  10. */
  11. class CacheAnalytics extends AnalyticsAbstract {
  12. constructor() {
  13. super();
  14. // some events may happen before init or implementation script download
  15. // in this case we accumulate them in this array and send them on init
  16. this.eventCache = [];
  17. }
  18. /**
  19. * Cache analytics event.
  20. * @param {String} action the name of the event
  21. * @param {Object} data can be any JSON object
  22. */
  23. sendEvent(action, data = {}) {
  24. this.eventCache.push({
  25. action,
  26. data
  27. });
  28. }
  29. /**
  30. * Clears the cached events.
  31. * @returns {Array} with the cached events.
  32. */
  33. drainCachedEvents() {
  34. const eventCacheCopy = this.eventCache.slice();
  35. this.eventCache = [];
  36. return eventCacheCopy;
  37. }
  38. }
  39. const cacheAnalytics = new CacheAnalytics();
  40. /**
  41. * This class will store and manage the handlers that are going to be used.
  42. */
  43. class AnalyticsAdapter {
  44. constructor() {
  45. this.analyticsHandlers = new Set();
  46. /**
  47. * Map of properties that will be added to every event
  48. */
  49. this.permanentProperties = Object.create(null);
  50. }
  51. /**
  52. * Initializes the AnalyticsAdapter. Adds the cacheAnalytics handler to
  53. * cache all the events until we have other handlers that are going to send
  54. * them.
  55. */
  56. init(browserName) {
  57. this.browserName = browserName;
  58. this.analyticsHandlers.add(cacheAnalytics);
  59. }
  60. /**
  61. * Sends analytics event.
  62. * @param {String} action the name of the event
  63. * @param {Object} data can be any JSON object
  64. */
  65. sendEvent(action, data = {}) {
  66. const modifiedData = Object.assign(
  67. {browserName: this.browserName}, this.permanentProperties, data);
  68. this.analyticsHandlers.forEach(
  69. analytics => analytics.sendEvent(action, modifiedData));
  70. }
  71. /**
  72. * Dispose analytics. Clears all handlers.
  73. */
  74. dispose() {
  75. cacheAnalytics.drainCachedEvents();
  76. this.analyticsHandlers.clear();
  77. }
  78. /**
  79. * Sets the handlers that are going to be used to send analytics and send
  80. * the cached events.
  81. * @param {Array} handlers the handlers
  82. */
  83. setAnalyticsHandlers(handlers) {
  84. this.analyticsHandlers = new Set(handlers);
  85. cacheAnalytics.drainCachedEvents().forEach(
  86. ev => this.sendEvent(ev.action, ev.data));
  87. }
  88. /**
  89. * Adds map of properties that will be added to every event.
  90. * @param {Object} properties the map of properties
  91. */
  92. addPermanentProperties(properties) {
  93. this.permanentProperties
  94. = Object.assign(this.permanentProperties, properties);
  95. }
  96. }
  97. export default new AnalyticsAdapter();