Browse Source

Adds caching of events to AnalyticsAdapter.

Use the caching from statistics, by calling loaded and dispose methods (onload and onerror).
dev1
damencho 9 years ago
parent
commit
643e8ca0ee
2 changed files with 55 additions and 8 deletions
  1. 45
    6
      modules/statistics/AnalyticsAdapter.js
  2. 10
    2
      modules/statistics/statistics.js

+ 45
- 6
modules/statistics/AnalyticsAdapter.js View File

7
     this.browserActionSuffix = '.' + RTCBrowserType.getBrowserName();
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
 // XXX Since we asynchronously load the integration of the analytics API and the
14
 // XXX Since we asynchronously load the integration of the analytics API and the
11
 // analytics API may asynchronously load its implementation (e.g. Google
15
 // analytics API may asynchronously load its implementation (e.g. Google
12
 // Analytics), we cannot make the decision with respect to which analytics
16
 // 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
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
     if (this.analytics === null || typeof this.analytics === 'undefined') {
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
     try {
35
     try {
23
         this.analytics.sendEvent(action + this.browserActionSuffix, data);
36
         this.analytics.sendEvent(action + this.browserActionSuffix, data);
24
     } catch (ignored) {}
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
 module.exports = new AnalyticsAdapter();
66
 module.exports = new AnalyticsAdapter();

+ 10
- 2
modules/statistics/statistics.js View File

48
         customScriptUrl ? customScriptUrl : 'analytics.js',
48
         customScriptUrl ? customScriptUrl : 'analytics.js',
49
         /* async */ true,
49
         /* async */ true,
50
         /* prepend */ false,
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
 
106
 
101
     if (Statistics.disableThirdPartyRequests !== true)
107
     if (Statistics.disableThirdPartyRequests !== true)
102
         loadAnalytics(options.analyticsScriptUrl);
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
 function Statistics(xmpp, options) {
113
 function Statistics(xmpp, options) {
106
     this.rtpStats = null;
114
     this.rtpStats = null;

Loading…
Cancel
Save