|
@@ -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();
|