您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AnalyticsAdapter.js 3.0KB

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