Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AnalyticsAdapter.js 3.1KB

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