forked from CaliStyle/ng-intercom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
intercom.browser.ts
76 lines (73 loc) · 1.73 KB
/
intercom.browser.ts
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
/**
* Intercom Browser module for Angular
*/
export class IntercomBrowser {
app_id: string;
init (data, onLoad?, onError?) {
if (data.app_id) {
this.app_id = data.app_id;
}
let w = (<any> window);
// Intercom initialization
w.intercomSettings = data;
if (w.Intercom === undefined){
w.Intercom = {
q: [
['boot', data],
],
};
this.loader(onLoad, onError);
} else {
this.boot(data);
}
}
sendCmd (cmd: string, data?: any) {
let w = (<any> window);
// Delay sending requests until Intercom is initialized
if (w.Intercom === undefined) {
console.error('ERROR Intercom may be blocked');
} else if (typeof w.Intercom === 'function') {
w.Intercom(cmd, data);
} else {
w.Intercom.q.push([cmd, data]);
}
}
boot (data) {
this.sendCmd('boot', data);
}
update (data) {
this.sendCmd('update', data);
}
shutdown () {
this.sendCmd('shutdown');
}
showNewMessage (msg?: string) {
this.sendCmd('showNewMessage', msg);
}
trackEvent (name: string, data?: any) {
let w = (<any> window);
// Event tracking takes 3 arguments, so we split this out
// into a new function instead of just calling sendCmd
if (w.Intercom === undefined) {
console.error('ERROR Intercom may be blocked');
} else if (typeof w.Intercom === 'function') {
w.Intercom('trackEvent', name, data);
} else {
w.Intercom.q.push(['trackEvent', name, data]);
}
}
loader (onLoad?, onError?) {
let s = document.createElement('script');
s.type='text/javascript';
s.async=true;
s.src=`https://widget.intercom.io/widget/${this.app_id}`;
if (onLoad) {
s.onload = onLoad;
}
if (onError) {
s.onerror = onError;
}
let x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s,x);
}
}