forked from zooniverse/geordi-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
270 lines (240 loc) · 10.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Generated by CoffeeScript 1.10.0
(function() {
var GeordiClient, ZooUserStringGetter,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
ZooUserStringGetter = require('zooniverse-user-string-getter');
module.exports = GeordiClient = (function() {
GeordiClient.prototype.GEORDI_STAGING_SERVER_URL = 'https://geordi.staging.zooniverse.org/api/events/';
GeordiClient.prototype.GEORDI_PRODUCTION_SERVER_URL = 'https://geordi.zooniverse.org/api/events/';
GeordiClient.prototype.gettingCohort = false;
GeordiClient.prototype._defaultSubjectGetter = function() {
return "(N/A)";
};
GeordiClient.prototype._defaultSubjectGetterParameter = function() {
return "(N/A)";
};
GeordiClient.prototype._defaultLastKnownCohortGetter = function() {
return null;
};
GeordiClient.prototype._defaultZooUserIDGetter = function() {
return null;
};
GeordiClient.prototype._defaultProjectToken = "unspecified";
GeordiClient.prototype._getCurrentSubject = GeordiClient._defaultSubjectGetter;
GeordiClient.prototype._getCurrentUserID = GeordiClient._defaultZooUserIDGetter;
function GeordiClient(config) {
this.logEvent = bind(this.logEvent, this);
this._buildEventData = bind(this._buildEventData, this);
this._addCohortToEventData = bind(this._addCohortToEventData, this);
this._addUserDetailsToEventData = bind(this._addUserDetailsToEventData, this);
this._logToGeordi = bind(this._logToGeordi, this);
if (!"server" in config) {
config["server"] = "staging";
}
if ((!"projectToken" in config) || (!config["projectToken"] instanceof String) || (!config["projectToken"].length > 0)) {
config["projectToken"] = this._defaultProjectToken;
}
if ((!"zooUserIDGetter" in config) || (!config["zooUserIDGetter"] instanceof Function)) {
config["zooUserIDGetter"] = this._defaultZooUserIDGetter;
}
if ((!"subjectGetter" in config) || (!config["subjectGetter"] instanceof Function)) {
config["subjectGetter"] = this._defaultSubjectGetter;
}
if (!"subjectGetterParameter" in config) {
config["subjectGetterParameter"] = this._defaultSubjectGetterParameter;
}
if (config["server"] === "production") {
this.GEORDI_SERVER_URL = this.GEORDI_PRODUCTION_SERVER_URL;
} else {
this.GEORDI_SERVER_URL = this.GEORDI_STAGING_SERVER_URL;
}
if ("experimentServerClient" in config) {
this.experimentServerClient = config["experimentServerClient"];
}
this._getCurrentSubject = config["subjectGetter"];
this._getCurrentSubjectParameter = config["subjectGetterParameter"];
this._getCurrentUserID = config["zooUserIDGetter"];
this._getCurrentUserIDParameter = config["zooUserIDGetterParameter"];
this._projectToken = config["projectToken"];
this.UserStringGetter = new ZooUserStringGetter(this._getCurrentUserID, this._getCurrentUserIDParameter);
}
/*
log event with Google Analytics
*/
GeordiClient.prototype._logToGoogle = function(eventData) {
if (typeof dataLayer !== "undefined" && dataLayer !== null) {
return dataLayer.push({
event: "gaTriggerEvent",
project_token: eventData['projectToken'],
user_id: eventData['userID'],
subject_id: eventData['subjectID'],
geordi_event_type: eventData['type'],
classification_id: eventData['relatedID']
});
} else {
throw new Error("Warning: Google Tag Manager script not found - Geordi is not logging to Google Analytics.");
}
};
/*
log event with Geordi v2.1
*/
GeordiClient.prototype._logToGeordi = function(eventData) {
return $.ajax({
url: this.GEORDI_SERVER_URL,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(eventData),
dataType: 'json'
});
};
/*
add the user's details to the event data
*/
GeordiClient.prototype._addUserDetailsToEventData = function(eventData) {
var eventualEventData;
eventualEventData = new $.Deferred;
if ((!this.UserStringGetter.currentUserID) || this.UserStringGetter.currentUserID === this.UserStringGetter.ANONYMOUS) {
this.UserStringGetter.getUserID().then((function(_this) {
return function(data) {
if (data != null) {
if (data !== _this.UserStringGetter.currentUserID) {
return _this.UserStringGetter.currentUserID = data;
}
}
};
})(this)).always((function(_this) {
return function() {
eventData['userID'] = _this.UserStringGetter.currentUserID;
return eventualEventData.resolve(eventData);
};
})(this));
} else {
eventData['userID'] = this.UserStringGetter.currentUserID;
eventualEventData.resolve(eventData);
}
return eventualEventData.promise();
};
GeordiClient.prototype._addCohortToEventData = function(eventData) {
var eventualEventData;
eventualEventData = new $.Deferred;
this.experimentServerClient.getCohort().then((function(_this) {
return function(cohort) {
if (cohort != null) {
eventData['cohort'] = cohort;
return _this.experimentServerClient.currentCohort = cohort;
}
};
})(this)).always(function() {
return eventualEventData.resolve(eventData);
});
return eventualEventData.promise();
};
GeordiClient.prototype._buildEventData = function(eventData) {
if (eventData == null) {
eventData = {};
}
eventData['browserTime'] = Date.now();
eventData['projectToken'] = this._projectToken;
eventData['errorCode'] = "";
eventData['errorDescription'] = "";
if (this.experimentServerClient != null) {
eventData['experiment'] = this.experimentServerClient.ACTIVE_EXPERIMENT;
if (this.experimentServerClient.currentCohort != null) {
eventData['cohort'] = this.experimentServerClient.currentCohort;
}
}
if (this.UserStringGetter.currentUserID != null) {
eventData['userID'] = this.UserStringGetter.currentUserID;
} else {
eventData['userID'] = this.UserStringGetter.ANONYMOUS;
}
return eventData;
};
GeordiClient.prototype._updateEventDataFromParameterObject = function(parameterObject, eventData) {
var field, i, k, len, newData, ref, v;
if (eventData == null) {
eventData = {};
}
ref = ["userID", "subjectID", "relatedID", "errorCode", "errorDescription", "projectToken", "serverURL", "experiment", "cohort", "type", "userSeq", "sessionNumber", "eventNumber", "userAgent", "clientIP"];
for (i = 0, len = ref.length; i < len; i++) {
field = ref[i];
if (field in parameterObject && typeof parameterObject[field] === "string" && parameterObject[field].length > 0) {
eventData[field] = parameterObject[field];
}
}
if ("data" in parameterObject) {
if (typeof parameterObject["data"] === "string") {
newData = JSON.parse(parameterObject["data"]);
} else {
newData = parameterObject["data"];
}
if (eventData["data"] != null) {
if (typeof eventData["data"] === "string") {
eventData["data"] = JSON.parse(eventData["data"]);
}
for (k in newData) {
v = newData[k];
eventData["data"][k] = v;
}
} else {
eventData["data"] = newData;
}
eventData["data"] = JSON.stringify(eventData["data"]);
}
if ("browserTime" in parameterObject && typeof parameterObject["browserTime"] === "number" && parameterObject["browserTime"] > 1441062000000) {
eventData["browserTime"] = parameterObject["browserTime"];
}
return eventData;
};
/*
This will log a user interaction both in the Geordi
analytics API and in Google Analytics.
*/
GeordiClient.prototype.logEvent = function(parameter) {
var eventData;
eventData = this._buildEventData();
if (typeof parameter === "string") {
eventData["type"] = parameter;
} else if (typeof parameter === "object") {
if (!("type" in parameter && typeof parameter.type === "string" && parameter.type.length > 0)) {
eventData["errorCode"] = "GCP01";
eventData["errorDescription"] = "missing 'type' when calling logEvent in Geordi client";
eventData["type"] = "error";
} else {
eventData = this._updateEventDataFromParameterObject(parameter, eventData);
}
if (!("subjectID" in eventData && typeof parameter.subjectID === "string" && parameter.subjectID.length > 0)) {
eventData["subjectID"] = this._getCurrentSubject();
}
} else {
eventData["errorCode"] = "GCP02";
eventData["errorDescription"] = "bad parameter passed to logEvent in Geordi Client";
eventData["type"] = "error";
}
return this._addUserDetailsToEventData(eventData).always((function(_this) {
return function(eventData) {
if (eventData["userID"] == null) {
eventData["userID"] = _this.UserStringGetter.ANONYMOUS;
}
if ((_this.experimentServerClient == null) || (!_this.experimentServerClient.shouldGetCohort(eventData["userID"]))) {
_this._logToGeordi(eventData);
return _this._logToGoogle(eventData);
} else {
if (!_this.gettingCohort) {
_this.gettingCohort = true;
return _this._addCohortToEventData(eventData).always(function(eventData) {
_this._logToGeordi(eventData);
_this._logToGoogle(eventData);
return _this.gettingCohort = false;
});
} else {
_this._logToGeordi(eventData);
return _this._logToGoogle(eventData);
}
}
};
})(this));
};
return GeordiClient;
})();
}).call(this);