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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var RTCBrowserType = require("../RTC/RTCBrowserType");
  2. function NoopAnalytics() {}
  3. NoopAnalytics.prototype.sendEvent = function () {};
  4. function AnalyticsAdapter() {
  5. this.browserActionSuffix = '.' + 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) {
  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. });
  27. // stored, lets break here
  28. return;
  29. }
  30. }
  31. try {
  32. this.analytics.sendEvent(action + this.browserActionSuffix, data);
  33. } catch (ignored) {}
  34. };
  35. /**
  36. * Dispose analytics. Clears any available queue element and sets
  37. * NoopAnalytics to be used.
  38. */
  39. AnalyticsAdapter.prototype.dispose = function () {
  40. this.analytics = new NoopAnalytics();
  41. AnalyticsAdapter.eventsQueue.length = 0;
  42. };
  43. /**
  44. * Loaded analytics script. Sens queued events.
  45. */
  46. AnalyticsAdapter.prototype.loaded = function () {
  47. var AnalyticsImpl = window.Analytics || NoopAnalytics;
  48. this.analytics = new AnalyticsImpl();
  49. // new analytics lets send all events if any
  50. if (AnalyticsAdapter.eventsQueue.length) {
  51. AnalyticsAdapter.eventsQueue.forEach(function (event) {
  52. this.sendEvent(event.action, event.data);
  53. }.bind(this));
  54. AnalyticsAdapter.eventsQueue.length = 0;
  55. }
  56. };
  57. module.exports = new AnalyticsAdapter();