forked from openedx-unsupported/edx-analytics-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking-view.js
148 lines (121 loc) · 5.74 KB
/
tracking-view.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
define(['backbone', 'underscore', 'utils/utils'],
function (Backbone, _, Utils) {
'use strict';
/**
* This 'view' doesn't display anything, but rather sends tracking
* information in response to 'segment:track' events triggered by the
* model.
*
* Actions will only be tracked if segmentApplicationId is set in the
* model.
*/
var TrackingView = Backbone.View.extend({
/**
* Reference to segment.io analytics library. This is set after
* loading.
*/
segment: undefined,
events: {
'shown.bs.tooltip': 'trackElementEvent'
},
initialize: function (options) {
var self = this;
self.options = options;
// wait until you have a segment application ID before kicking
// up the script
if (self.model.isTracking()) {
self.applicationIdSet();
} else {
self.listenToOnce(self.model, 'change:segmentApplicationId',
self.applicationIdSet);
}
},
applicationIdSet: function () {
var self = this,
trackId = self.model.get('segmentApplicationId');
// if no ID is supplied, then don't track
if (self.model.isTracking()) {
// kick off segment
self.initSegment(trackId);
self.logUser();
// now segment has been loaded, we can track events
self.listenTo(self.model, 'segment:track', self.track);
}
},
/**
* This emits an event to our external tracking systems when an
* event bubbles up from a DOM element.
*/
trackElementEvent: function (ev) {
var self = this,
trackedElement = ev.target,
properties = Utils.getNodeProperties(
trackedElement.attributes, 'data-track-', ['data-track-event']),
eventType = $(trackedElement).attr('data-track-event');
if (!self.model.isTracking() || _.isEmpty(eventType) || !_.isString(eventType)) {
return;
}
self.track(eventType, properties);
},
/**
* This sets up segment.io for our application and loads the initial
* page load.
*
* this.segment is set for convenience.
*/
initSegment: function (applicationKey) {
var self = this;
if (_.isUndefined(self.segment)) {
// This is taken directly from https://segment.io/docs/tutorials/quickstart-analytics.js/.
/* jshint ignore:start */
// jscs:disable
window.analytics=window.analytics||[],window.analytics.methods=['identify','group','track','page','pageview','alias','ready','on','once','off','trackLink','trackForm','trackClick','trackSubmit'],window.analytics.factory=function(t){return function(){var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var key=window.analytics.methods[i];window.analytics[key]=window.analytics.factory(key)}window.analytics.load=function(t){if(!document.getElementById('analytics-js')){var a=document.createElement('script');a.type='text/javascript',a.id='analytics-js',a.async=!0,a.src=('https:'===document.location.protocol?'https://':'http://')+'cdn.segment.io/analytics.js/v1/'+t+'/analytics.min.js';var n=document.getElementsByTagName('script')[0];n.parentNode.insertBefore(a,n)}},window.analytics.SNIPPET_VERSION='2.0.9';
// jscs:enable
/* jshint ignore:end */
// shortcut to segment.io
self.segment = window.analytics;
}
// provide our application key for logging
self.segment.load(applicationKey);
// this needs to be called once
self.segment.page(self.buildCourseProperties());
},
/**
* Log the user.
*/
logUser: function () {
var self = this,
userModel = self.options.userModel;
self.segment.identify(userModel.get('username'), {
name: userModel.get('name'),
email: userModel.get('email'),
ignoreInReporting: userModel.get('ignoreInReporting')
});
},
buildCourseProperties: function() {
var self = this,
course = {};
if (self.options.courseModel) {
course.courseId = self.options.courseModel.get('courseId');
}
if (self.model.has('page')) {
course.label = self.model.get('page');
}
return course;
},
/**
* Catch 'segment:track' events and create events and send
* to segment.io.
*
* @param eventType String event type.
*/
track: function (eventType, properties) {
var self = this,
course = self.buildCourseProperties();
// send event to segment including the course ID
self.segment.track(eventType, _.extend(course, properties));
}
});
return TrackingView;
}
);