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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var RTCBrowserType = require("../RTC/RTCBrowserType");
  2. function NoopAnalytics() {}
  3. NoopAnalytics.prototype.sendEvent = function () {};
  4. function AnalyticsAdapter() {
  5. this.browserName = RTCBrowserType.getBrowserName();
  6. }
  7. // some events may happen before init or implementation script download
  8. // in this case we accumulate them in this array and send them on init
  9. AnalyticsAdapter.eventsQueue = [];
  10. /**
  11. * Sends analytics event.
  12. * @param action
  13. * @param data
  14. * @param label
  15. */
  16. AnalyticsAdapter.prototype.sendEvent = function (action, data, label) {
  17. if(this._checkAnalyticsAndMaybeCacheEvent(
  18. "sendEvent", action, data,label)) {
  19. try {
  20. this.analytics.sendEvent(action, data, label, this.browserName);
  21. } catch (ignored) { // eslint-disable-line no-empty
  22. }
  23. }
  24. };
  25. /**
  26. * Sends feedback.
  27. * @param {object} data with proprties:
  28. * - {int} overall an integer between 1 and 5 indicating the user feedback
  29. * - {string} detailed detailed feedback from the user.
  30. * @param label
  31. */
  32. AnalyticsAdapter.prototype.sendFeedback = function (data, label) {
  33. if(this._checkAnalyticsAndMaybeCacheEvent(
  34. "sendFeedback", null, data,label)) {
  35. try {
  36. this.analytics.sendFeedback(data, label, this.browserName);
  37. } catch (ignored) { // eslint-disable-line no-empty
  38. }
  39. }
  40. };
  41. /**
  42. * Since we asynchronously load the integration of the analytics API and the
  43. * analytics API may asynchronously load its implementation (e.g. Google
  44. * Analytics), we cannot make the decision with respect to which analytics
  45. * implementation we will use here and we have to postpone it i.e. we will make
  46. * a lazy decision, will wait for loaded or dispose methods to be called.
  47. * in the meantime we accumulate any events received. We should call this
  48. * method before trying to send the event.
  49. * @param {string} method - Identifies which method should we use later for the
  50. * cached events - "sendEvent" or "sendFeedback".
  51. * @param action
  52. * @param data
  53. * @param label
  54. */
  55. AnalyticsAdapter.prototype._checkAnalyticsAndMaybeCacheEvent
  56. = function (method, action, data, label) {
  57. if (this.analytics === null || typeof this.analytics === 'undefined') {
  58. // missing this.analytics but have window implementation, let's use it
  59. if (window.Analytics) {
  60. this.loaded();
  61. }
  62. else {
  63. AnalyticsAdapter.eventsQueue.push({
  64. method: method,
  65. action: action,
  66. data: data,
  67. label: label
  68. });
  69. // stored, lets break here
  70. return false;
  71. }
  72. }
  73. return true;
  74. };
  75. /**
  76. * Dispose analytics. Clears any available queue element and sets
  77. * NoopAnalytics to be used.
  78. */
  79. AnalyticsAdapter.prototype.dispose = function () {
  80. this.analytics = new NoopAnalytics();
  81. AnalyticsAdapter.eventsQueue.length = 0;
  82. };
  83. /**
  84. * Loaded analytics script. Sens queued events.
  85. */
  86. AnalyticsAdapter.prototype.loaded = function () {
  87. var AnalyticsImpl = window.Analytics || NoopAnalytics;
  88. this.analytics = new AnalyticsImpl();
  89. // new analytics lets send all events if any
  90. if (AnalyticsAdapter.eventsQueue.length) {
  91. AnalyticsAdapter.eventsQueue.forEach(function (event) {
  92. switch(event.method) {
  93. case "sendEvent":
  94. this.sendEvent(event.action, event.data, event.label);
  95. break;
  96. case "sendFeedback":
  97. this.sendFeedback(event.data, event.label);
  98. break;
  99. }
  100. }.bind(this));
  101. AnalyticsAdapter.eventsQueue.length = 0;
  102. }
  103. };
  104. module.exports = new AnalyticsAdapter();