-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathintercom_client.js
69 lines (58 loc) · 1.66 KB
/
intercom_client.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
// We *must* have the intercom id set otherwise the intercom loader script throws
// exceptions. Warn people about this.
Meteor.startup(function() {
if (! Meteor._get(Meteor, 'settings', 'public', 'intercom', 'id')) {
console.warn("You must set Meteor.settings.public.intercom.id to use percolate:intercom");
}
});
Meteor.subscribe('currentUserIntercomHash');
var minimumUserInfo = function(user) {
var info = {
app_id: Meteor._get(Meteor, 'settings', 'public', 'intercom', 'id')
};
if (user)
_.extend(info, {
user_id: user._id,
created_at: Math.round(user.createdAt/1000),
user_hash: user.intercomHash
});
return info;
};
IntercomSettings = {
// if you want to manually call it
minimumUserInfo: minimumUserInfo
};
var booted = false;
// send data to intercom
Meteor.startup(function() {
Tracker.autorun(function() {
var user = Meteor.user();
var info = IntercomSettings.minimumUserInfo(user);
var ready;
if (!user) {
if (Meteor._get(Meteor, 'settings', 'public', 'intercom', 'allowAnonymous') === true) {
if (IntercomSettings.anonymousInfo) {
ready = IntercomSettings.anonymousInfo(info);
if (ready === false)
return;
}
} else {
// console.log('shutdown')
booted = false;
return Intercom('shutdown');
}
} else {
if (IntercomSettings.userInfo) {
ready = IntercomSettings.userInfo(user, info);
if (ready === false)
return;
}
}
if (info) {
var type = booted ? 'update' : 'boot';
// console.log(type, info)
Intercom(type, info);
booted = true;
}
});
});