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 3.2KB

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