|
@@ -5,6 +5,10 @@ NoopAnalytics.prototype.sendEvent = function () {};
|
5
|
5
|
|
6
|
6
|
function AnalyticsAdapter() {
|
7
|
7
|
this.browserName = RTCBrowserType.getBrowserName();
|
|
8
|
+ /**
|
|
9
|
+ * Map of properties that will be added to every event
|
|
10
|
+ */
|
|
11
|
+ this.permanentProperties = {};
|
8
|
12
|
}
|
9
|
13
|
|
10
|
14
|
// some events may happen before init or implementation script download
|
|
@@ -13,36 +17,18 @@ AnalyticsAdapter.eventsQueue = [];
|
13
|
17
|
|
14
|
18
|
/**
|
15
|
19
|
* Sends analytics event.
|
16
|
|
- * @param action
|
17
|
|
- * @param data
|
18
|
|
- * @param label
|
19
|
|
- */
|
20
|
|
-AnalyticsAdapter.prototype.sendEvent = function (action, data, label) {
|
21
|
|
- if(this._checkAnalyticsAndMaybeCacheEvent(
|
22
|
|
- "sendEvent", action, data,label)) {
|
23
|
|
- try {
|
24
|
|
- this.analytics.sendEvent(action, data, label, this.browserName);
|
25
|
|
- } catch (ignored) { // eslint-disable-line no-empty
|
26
|
|
- }
|
27
|
|
- }
|
28
|
|
-};
|
29
|
|
-
|
30
|
|
-/**
|
31
|
|
- * Sends feedback.
|
32
|
|
- * @param {object} data with proprties:
|
33
|
|
- * - {int} overall an integer between 1 and 5 indicating the user feedback
|
34
|
|
- * - {string} detailed detailed feedback from the user.
|
35
|
|
- * @param label
|
|
20
|
+ * @param {String} action the name of the event
|
|
21
|
+ * @param {Object} data can be any JSON object
|
36
|
22
|
*/
|
37
|
|
-AnalyticsAdapter.prototype.sendFeedback = function (data, label) {
|
38
|
|
- if(this._checkAnalyticsAndMaybeCacheEvent(
|
39
|
|
- "sendFeedback", null, data,label)) {
|
|
23
|
+AnalyticsAdapter.prototype.sendEvent = function (action, data = {}) {
|
|
24
|
+ if(this._checkAnalyticsAndMaybeCacheEvent(action, data)) {
|
|
25
|
+ data.browserName = this.browserName;
|
40
|
26
|
try {
|
41
|
|
- this.analytics.sendFeedback(data, label, this.browserName);
|
|
27
|
+ this.analytics.sendEvent(action,
|
|
28
|
+ Object.assign({}, this.permanentProperties, data));
|
42
|
29
|
} catch (ignored) { // eslint-disable-line no-empty
|
43
|
30
|
}
|
44
|
31
|
}
|
45
|
|
-
|
46
|
32
|
};
|
47
|
33
|
|
48
|
34
|
/**
|
|
@@ -53,14 +39,11 @@ AnalyticsAdapter.prototype.sendFeedback = function (data, label) {
|
53
|
39
|
* a lazy decision, will wait for loaded or dispose methods to be called.
|
54
|
40
|
* in the meantime we accumulate any events received. We should call this
|
55
|
41
|
* method before trying to send the event.
|
56
|
|
- * @param {string} method - Identifies which method should we use later for the
|
57
|
|
- * cached events - "sendEvent" or "sendFeedback".
|
58
|
42
|
* @param action
|
59
|
43
|
* @param data
|
60
|
|
- * @param label
|
61
|
44
|
*/
|
62
|
45
|
AnalyticsAdapter.prototype._checkAnalyticsAndMaybeCacheEvent
|
63
|
|
-= function (method, action, data, label) {
|
|
46
|
+= function (action, data) {
|
64
|
47
|
if (this.analytics === null || typeof this.analytics === 'undefined') {
|
65
|
48
|
// missing this.analytics but have window implementation, let's use it
|
66
|
49
|
if (window.Analytics) {
|
|
@@ -68,10 +51,8 @@ AnalyticsAdapter.prototype._checkAnalyticsAndMaybeCacheEvent
|
68
|
51
|
}
|
69
|
52
|
else {
|
70
|
53
|
AnalyticsAdapter.eventsQueue.push({
|
71
|
|
- method: method,
|
72
|
54
|
action: action,
|
73
|
|
- data: data,
|
74
|
|
- label: label
|
|
55
|
+ data: data
|
75
|
56
|
});
|
76
|
57
|
// stored, lets break here
|
77
|
58
|
return false;
|
|
@@ -90,6 +71,15 @@ AnalyticsAdapter.prototype.dispose = function () {
|
90
|
71
|
AnalyticsAdapter.eventsQueue.length = 0;
|
91
|
72
|
};
|
92
|
73
|
|
|
74
|
+/**
|
|
75
|
+ * Adds map of properties that will be added to every event.
|
|
76
|
+ * @param {Object} properties the map of properties
|
|
77
|
+ */
|
|
78
|
+AnalyticsAdapter.prototype.addPermanentProperties = function (properties) {
|
|
79
|
+ this.permanentProperties
|
|
80
|
+ = Object.assign(this.permanentProperties, properties);
|
|
81
|
+};
|
|
82
|
+
|
93
|
83
|
/**
|
94
|
84
|
* Loaded analytics script. Sens queued events.
|
95
|
85
|
*/
|
|
@@ -101,15 +91,7 @@ AnalyticsAdapter.prototype.loaded = function () {
|
101
|
91
|
// new analytics lets send all events if any
|
102
|
92
|
if (AnalyticsAdapter.eventsQueue.length) {
|
103
|
93
|
AnalyticsAdapter.eventsQueue.forEach(function (event) {
|
104
|
|
- switch(event.method) {
|
105
|
|
- case "sendEvent":
|
106
|
|
- this.sendEvent(event.action, event.data, event.label);
|
107
|
|
- break;
|
108
|
|
- case "sendFeedback":
|
109
|
|
- this.sendFeedback(event.data, event.label);
|
110
|
|
- break;
|
111
|
|
- }
|
112
|
|
-
|
|
94
|
+ this.sendEvent(event.action, event.data);
|
113
|
95
|
}.bind(this));
|
114
|
96
|
AnalyticsAdapter.eventsQueue.length = 0;
|
115
|
97
|
}
|