This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
308 lines (257 loc) · 7.43 KB
/
Copy pathindex.js
File metadata and controls
308 lines (257 loc) · 7.43 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict';
/**
* Module dependencies.
*/
var convertDates = require('@segment/convert-dates');
var defaults = require('@ndhoule/defaults');
var del = require('obj-case').del;
var integration = require('@segment/analytics.js-integration');
var is = require('is');
var extend = require('@ndhoule/extend');
var clone = require('@ndhoule/clone');
var each = require('@ndhoule/each');
var pick = require('@ndhoule/pick');
/**
* Expose `Intercom` integration.
*/
var Intercom = module.exports = integration('Intercom')
.global('Intercom')
.option('activator', '#IntercomDefaultWidget')
.option('appId', '')
.option('richLinkProperties', [])
.tag('<script src="https://widget.intercom.io/widget/{{ appId }}">');
/**
* Initialize.
*
* http://docs.intercom.io/
* http://docs.intercom.io/#IntercomJS
*
* @api public
*/
Intercom.prototype.initialize = function() {
// Shim out the Intercom library.
window.Intercom = function() {
window.Intercom.q.push(arguments);
};
window.Intercom.q = [];
this.load(this.ready);
};
/**
* Loaded?
*
* @api private
* @return {boolean}
*/
Intercom.prototype.loaded = function() {
return typeof window.Intercom === 'function';
};
/**
* Page.
*
* @api public
* @param {Page} page
*/
Intercom.prototype.page = function(page) {
var integrationSettings = page.options(this.name);
this.bootOrUpdate({}, integrationSettings);
};
/**
* Identify.
*
* http://docs.intercom.io/#IntercomJS
*
* @api public
* @param {Identify} identify
*/
Intercom.prototype.identify = function(identify) {
var traits = identify.traits({ userId: 'user_id' });
var integrationSettings = identify.options(this.name);
var companyCreated = identify.companyCreated();
var created = identify.created();
var name = identify.name();
var id = identify.userId();
var group = this.analytics.group();
var settings = this.options;
if (!id && !identify.email()) {
return;
}
// intercom requires `company` to be an object. default it with group traits
// so that we guarantee an `id` is there, since they require it
if (traits.company !== null && !is.object(traits.company)) {
delete traits.company;
}
if (traits.company) {
defaults(traits.company, group.traits());
}
// name
if (name) traits.name = name;
// handle dates
if (created) {
del(traits, 'created');
del(traits, 'createdAt');
traits.created_at = created;
}
if (companyCreated) {
del(traits.company, 'created');
del(traits.company, 'createdAt');
traits.company.created_at = companyCreated;
}
// convert dates
traits = convertDates(traits, formatDate);
// format nested custom traits
traits = formatNestedCustomTraits(traits, settings);
// handle options
if (integrationSettings.userHash) traits.user_hash = integrationSettings.userHash;
if (integrationSettings.user_hash) traits.user_hash = integrationSettings.user_hash;
this.bootOrUpdate(traits, integrationSettings);
};
/**
* Group.
*
* @api public
* @param {Group} group
*/
Intercom.prototype.group = function(group) {
var settings = this.options;
// using .traits here since group.properties() doesn't take alias object
var props = group.traits({
createdAt: 'created',
created: 'created_at',
monthlySpend: 'monthly_spend'
});
props = convertDates(props, formatDate);
var id = group.groupId();
if (id) props.id = id;
var integrationSettings = group.options(this.name);
// format nested custom traits
props = formatNestedCustomTraits(props, settings);
var traits = extend({ company: props }, hideDefaultLauncher(integrationSettings));
api('update', traits);
};
/**
* Track.
*
* @api public
* @param {Track} track
*/
Intercom.prototype.track = function(track) {
var settings = this.options;
var props = track.properties();
var revenue = track.revenue();
if (revenue) {
var revenueData = {
// Intercom requests value in cents
price: {
amount: revenue * 100,
currency: track.currency() // fallsback on 'USD'
}
};
}
// format Nested custom traits
props = formatNestedCustomTraits(props, settings);
props = extend(props, revenueData);
del(props, 'revenue');
del(props, 'currency');
api('trackEvent', track.event(), props);
};
/**
* Boots or updates, as appropriate.
*
* @api private
* @param {Object} options
*/
Intercom.prototype.bootOrUpdate = function(options, integrationSettings) {
options = options || {};
var method = this.booted === true ? 'update' : 'boot';
var activator = this.options.activator;
options.app_id = this.options.appId;
options.language_override = integrationSettings.language_override;
// Intercom, will force the widget to appear if the selector is
// #IntercomDefaultWidget so no need to check inbox, just need to check that
// the selector isn't #IntercomDefaultWidget.
if (activator !== '#IntercomDefaultWidget') {
options.widget = { activator: activator };
}
// Check for selective showing of messenger option
options = extend(options, hideDefaultLauncher(integrationSettings));
api(method, options);
this.booted = true;
};
/**
* Format a date to Intercom's liking.
*
* @api private
* @param {Date} date
* @return {number}
*/
function formatDate(date) {
return Math.floor(date / 1000);
}
/**
* Flatten selectively based on your settings. You can either stringify, flatten, or drop the properties.
* Intercom rejects nested objects so you must choose a method.
*
* @param {Object} obj
* @param {Object} settings
* @return {Object} ret
* @api private
*/
function formatNestedCustomTraits(obj, settings) {
var richLinkProperties = settings.richLinkProperties;
var basicIntercomTraits = [
'companies',
'company',
'created_at',
'created',
'custom_attributes',
'company_id',
'id',
'name',
'monthly_spend',
'plan',
'remote_created_at',
'remove',
'user_id',
'email'
];
// add rich link object to semantic traits so that it's not altered by the default method and
// is passed to intercom as a nested object: https://developers.intercom.com/reference#event-metadata-types
var semanticTraits = basicIntercomTraits.concat(richLinkProperties);
// clone traits so we don't modify the original object
var customTraits = clone(obj);
// filter out semanticTraits so that we only format custom nested traits
each(function(trait) {
del(customTraits, trait);
}, semanticTraits);
// create object without custom traits to merge with formatted custom traits in the end
var standardTraits = pick(semanticTraits, obj);
// drop any arrays or objects
var supportedTraits = {};
each(function(value, key) {
if (!is.object(value) && !is.array(value)) supportedTraits[key] = value;
}, customTraits);
// combine all the traits
return extend(supportedTraits, standardTraits);
}
/**
* Push a call onto the Intercom queue.
*
* @api private
*/
function api() {
window.Intercom.apply(window.Intercom, arguments);
}
/**
* Selectively hide messenger
* https://docs.intercom.io/configure-intercom-for-your-product-or-site/customize-the-intercom-messenger/customize-the-intercom-messenger-technical#show-the-intercom-messenger-to-selected-users-for-web-
* @param {Object} options
* @return {Object} ret
* @api private
*/
function hideDefaultLauncher(options) {
var ret = {};
var setting = options.hideDefaultLauncher;
if (setting === undefined || typeof setting !== 'boolean') return ret;
ret.hide_default_launcher= setting;
return ret;
}