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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // XXX Since we asynchronously load the integration of the analytics API and the
  11. // analytics API may asynchronously load its implementation (e.g. Google
  12. // Analytics), we cannot make the decision with respect to which analytics
  13. // implementation we will use here and we have to postpone it i.e. we will make
  14. // a lazy decision, will wait for loaded or dispose methods to be called.
  15. // in the meantime we accumulate any events received
  16. AnalyticsAdapter.prototype.sendEvent = function (action, data, label) {
  17. if (this.analytics === null || typeof this.analytics === 'undefined') {
  18. // missing this.analytics but have window implementation, let's use it
  19. if (window.Analytics) {
  20. this.loaded();
  21. }
  22. else {
  23. AnalyticsAdapter.eventsQueue.push({
  24. action: action,
  25. data: data,
  26. label: label
  27. });
  28. // stored, lets break here
  29. return;
  30. }
  31. }
  32. try {
  33. this.analytics.sendEvent(action, data, label, this.browserName);
  34. } catch (ignored) { // eslint-disable-line no-empty
  35. }
  36. };
  37. /**
  38. * Dispose analytics. Clears any available queue element and sets
  39. * NoopAnalytics to be used.
  40. */
  41. AnalyticsAdapter.prototype.dispose = function () {
  42. this.analytics = new NoopAnalytics();
  43. AnalyticsAdapter.eventsQueue.length = 0;
  44. };
  45. /**
  46. * Loaded analytics script. Sens queued events.
  47. */
  48. AnalyticsAdapter.prototype.loaded = function () {
  49. var AnalyticsImpl = window.Analytics || NoopAnalytics;
  50. this.analytics = new AnalyticsImpl();
  51. // new analytics lets send all events if any
  52. if (AnalyticsAdapter.eventsQueue.length) {
  53. AnalyticsAdapter.eventsQueue.forEach(function (event) {
  54. this.sendEvent(event.action, event.data, event.label);
  55. }.bind(this));
  56. AnalyticsAdapter.eventsQueue.length = 0;
  57. }
  58. };
  59. module.exports = new AnalyticsAdapter();