Преглед на файлове

Adds caching of events to AnalyticsAdapter.

Use the caching from statistics, by calling loaded and dispose methods (onload and onerror).
dev1
damencho преди 8 години
родител
ревизия
643e8ca0ee
променени са 2 файла, в които са добавени 55 реда и са изтрити 8 реда
  1. 45
    6
      modules/statistics/AnalyticsAdapter.js
  2. 10
    2
      modules/statistics/statistics.js

+ 45
- 6
modules/statistics/AnalyticsAdapter.js Целия файл

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

+ 10
- 2
modules/statistics/statistics.js Целия файл

@@ -48,7 +48,13 @@ function loadAnalytics(customScriptUrl) {
48 48
         customScriptUrl ? customScriptUrl : 'analytics.js',
49 49
         /* async */ true,
50 50
         /* prepend */ false,
51
-        /* relativeURL */ customScriptUrl ? false : true);
51
+        /* relativeURL */ customScriptUrl ? false : true,
52
+        /* loadCallback */ function () {
53
+            Statistics.analytics.loaded();
54
+        },
55
+        /* errorCallback */ function () {
56
+            Statistics.analytics.dispose();
57
+        });
52 58
 }
53 59
 
54 60
 /**
@@ -100,7 +106,9 @@ Statistics.init = function (options) {
100 106
 
101 107
     if (Statistics.disableThirdPartyRequests !== true)
102 108
         loadAnalytics(options.analyticsScriptUrl);
103
-}
109
+    else // if not enable make sure we dispose any event that goes in the queue
110
+        Statistics.analytics.dispose();
111
+};
104 112
 
105 113
 function Statistics(xmpp, options) {
106 114
     this.rtpStats = null;

Loading…
Отказ
Запис