This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathanalytics.coffee
84 lines (77 loc) · 2.41 KB
/
analytics.coffee
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
$ = require 'jqueryify'
Subject = require 'models/subject'
UserGetter = require 'lib/userID'
buildEventData = (params) ->
eventData = {}
# defaults
eventData['subjectID'] = Subject.current?.zooniverse_id
eventData['projectToken'] = Subject.projectName
eventData['projectToken'] = "galaxy_zoo"
eventData['userID'] = "(anonymous)"
# set fields from params
eventData['time'] = Date.now()
eventData['projectToken'] = params.projectToken if params.projectToken?
eventData['userID'] = params.userID if params.userID?
eventData['subjectID'] = params.subjectID if params.subjectID?
eventData['type'] = params.type
eventData['relatedID'] = params.relatedID if params.relatedID?
eventData['experiment'] = params.experiment if params.experiment?
eventData['errorCode'] = ""
eventData['errorDescription'] = ""
eventData['cohort'] = params.cohort if params.cohort?
eventData
addUserDetailsToEventData = (eventData) ->
eventualUserIdentifier = new $.Deferred
UserGetter.getUserIDorIPAddress()
.then (data) =>
if data?
UserGetter.currentUserID = data
.fail =>
UserGetter.currentUserID = "(anonymous)"
.always =>
eventData['userID'] = UserGetter.currentUserID
eventualUserIdentifier.resolve eventData
eventualUserIdentifier.promise()
###
log event with Geordi v2
###
logToGeordi = (eventData) =>
$.ajax {
url: 'http://geordi.zooniverse.org/api/events/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(eventData),
dataType: 'json'
}
###
log event with Google Analytics
###
logToGoogle = (eventData) =>
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']
}
###
This will log a user interaction both in the Geordi analytics API and in Google Analytics.
###
logEvent = (params) =>
eventData = buildEventData(params)
addUserDetailsToEventData(eventData)
.always (eventData) =>
logToGeordi eventData
logToGoogle eventData
###
This will log an error in Geordi only.
###
logError = (params) ->
eventData = buildEventData(params)
eventData['errorCode'] = params.errorCode
eventData['type'] = "error"
eventData['errorDescription'] = params.errorDescription
logToGeordi eventData
exports.logEvent = logEvent
exports.logError = logError