-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserviceEventTracking.js
More file actions
274 lines (255 loc) · 6.93 KB
/
serviceEventTracking.js
File metadata and controls
274 lines (255 loc) · 6.93 KB
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
271
272
273
274
/**
* Wrapper of event-tracking functionality; making use of various
* 3rd party analytics platforms.
* - Intercom
* - Mixpanel
*/
'use strict';
require('app')
.service('eventTracking', EventTracking);
var User = require('@runnable/api-client/lib/models/user');
var _keypather;
var _$location;
var INTERCOM_APP_ID;
/**
* EventTracking
* @class
*/
function EventTracking(
$browser,
$location,
$log,
$state,
$stateParams,
$window,
assign,
keypather,
intercomAppId
) {
INTERCOM_APP_ID = intercomAppId;
_keypather = keypather;
_$location = $location;
this._Intercom = $window.Intercom;
this._user = null;
this.$window = $window;
/**
* Extend per-event data with specific properties
* to be sent w/ all events
* @param {Object} data - data for given event to be extended
* @return Object - extended event object
*/
this.extendEventData = function (data) {
if (!this._user) {
$log.error('eventTracking.boot() must be invoked before reporting events');
}
// username owner if server page
// name of server if server page
// page event triggered from
var baseData = {
state: $state.$current.name,
href: $window.location.href
};
if (angular.isFunction(keypather.get(this._user, 'oauthName'))) {
baseData.userName = this._user.oauthName();
}
if ($stateParams.userName) {
baseData.instanceOwner = $stateParams.userName;
}
if ($stateParams.instanceName) {
baseData.instanceName = $stateParams.instanceName;
}
return assign(data, baseData);
};
/**
* Stub Intercom when SDK not present
* (development/staging environments)
*/
if (!this._Intercom || $browser.cookies().isModerating) {
// stub intercom if not present
this._Intercom = angular.noop;
}
/**
* Wrap invokations of mixpanel SDK API methods (object properties)
* @param {String} mixpanel SDK API method name
* @params [1..n] optional arguments passed to mixpanel SDK
*/
this._mixpanel = function () {
if (!angular.isFunction(keypather.get($window, 'mixpanel.'+arguments[0]))) {
// $log.info('Mixpanel JS SDK stubbed');
// $log.info(arguments);
return;
}
var args = Array.prototype.slice.call(arguments);
var path = args[0].split('.');
// contextPath: "foo.bar.biz.bang" -> "foo.bar.biz" || "foo.bar.biz" -> "foo.bar"
var contextPath = path.slice(0, path.length - 1).join('');
var context = keypather.get($window.mixpanel, contextPath);
keypather.get($window, 'mixpanel.' + arguments[0])
.apply(context, args.slice(1, args.length));
};
}
/**
* Intercom and Mixpanel user identification
* @throws Error
* @param {Object} user - User Model instance
* @return this
*/
EventTracking.prototype.boot = function (user, opts) {
opts = opts || {};
if (this._user) { return this; }
if (!(user instanceof User)) {
throw new Error('arguments[0] must be instance of User');
}
if (user.attrs._beingModerated) {
user = new User(user.attrs._beingModerated, { noStore: true });
} else {
if (this.$window.fbq) {
this.$window.fbq('track', 'ViewContent', {
action: 'LoggedIn'
});
}
}
this._user = user;
var data = {
name: user.oauthName(),
email: user.attrs.email,
created_at: new Date(user.attrs.created) / 1000 || 0,
app_id: INTERCOM_APP_ID
};
if (opts.orgName) {
data.company = {
id: opts.orgName.toLowerCase(),
name: opts.orgName
};
}
// Mixpanel uses a string GUID to track anon users
// If we're still tracking the user via GUID, we need to alias
// Otherwise, we can just identify ourselves
if (angular.isString(this._mixpanel('get_distinct_id'))) {
this._mixpanel('alias', user.oauthId());
} else {
this._mixpanel('identify', user.oauthId());
}
this._Intercom('boot', data);
var userJSON = user.toJSON();
var firstName = '';
var lastName = '';
var displayName = _keypather.get(userJSON, 'accounts.github.displayName');
if (displayName) {
firstName = displayName.split(/ (.+)/)[0];
lastName = displayName.split(/ (.+)/)[1];
}
this._mixpanel('people.set', {
'$first_name': firstName,
'$last_name': lastName,
'$created': _keypather.get(userJSON, 'created'),
'$email': _keypather.get(userJSON, 'email')
});
return this;
};
/**
* Record user event toggling of selected commit in repository
* Reports to:
* - mixpanel
* @param {Object} data - key/value pairs of event data
* - keys
* - triggeredBuild: Boolean
* - slectedCommit: Object (ACV Model)
* @return this
*/
EventTracking.prototype.toggledCommit = function (data) {
var eventName = 'toggled-commit';
var eventData = this.extendEventData({
triggeredBuild: !!data.triggeredBuild,
selectedCommit: data.acv
});
this._mixpanel('track', eventName, eventData);
return this;
};
/**
* Record user-initiated build triggered event from throughout UI
* Reports to:
* - intercom
* - mixpanel
* @param {Boolean} cache - build triggered without cache
* @return this
*/
EventTracking.prototype.triggeredBuild = function (cache) {
var eventName = 'triggered-build';
var eventData = this.extendEventData({
cache: cache
});
this._Intercom('trackEvent', eventName, eventData);
this._mixpanel('track', eventName, eventData);
return this;
};
/**
* Record user visit to states
* Reports to:
* - mixpanel
* @return this
*/
EventTracking.prototype.visitedState = function () {
var eventName = 'visited-state';
var eventData = this.extendEventData({
referral: _$location.search().ref || 'direct'
});
this._mixpanel('track', eventName, eventData);
return this;
};
/**
* Intercom JS SDK API update method wrapper
* Checks for & displays new messages from Intercom
* @return this
*/
EventTracking.prototype.update = function () {
this._Intercom('update');
return this;
};
/**
* Track clicks on the page
* @param data
* @returns {EventTracking}
*/
EventTracking.prototype.trackClicked = function (data) {
this._mixpanel('track', 'clicked - ' + _keypather.get(data, 'text'), data);
return this;
};
/**
* Track creating repo containers
* @param {String} orgName
* @param {String} repoName
* @returns {EventTracking}
*/
EventTracking.prototype.createdRepoContainer = function (org, repo) {
if (this._mixpanel) {
this._mixpanel('track', 'createRepoContainer', {
org: org,
repo: repo
});
}
if (this.$window.fbq) {
this.$window.fbq('track', 'ViewContent', {
action: 'CreateContainer',
type: 'Repo'
});
}
};
/**
* Track creating non repo containers
* @param {String} containerName
* @returns {EventTracking}
*/
EventTracking.prototype.createdNonRepoContainer = function (containerName) {
if (this._mixpanel) {
this._mixpanel('track', 'createNonRepoContainer', {
containerName: containerName
});
}
if (this.$window.fbq) {
this.$window.fbq('track', 'ViewContent', {
action: 'CreateContainer',
type: 'NonRepo'
});
}
};