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

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