-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathAnalytics.controller.ts
More file actions
59 lines (49 loc) · 1.91 KB
/
Analytics.controller.ts
File metadata and controls
59 lines (49 loc) · 1.91 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
import { App } from '../app'
import { TonConnectObserver } from "../observers/TonConnect.observer";
import { DocumentObserver } from "../observers/Document.observer";
import { BACKEND_URL, STAGING_BACKEND_URL } from "../constants";
import { WebViewObserver } from "../observers/WebView.observer";
export class AnalyticsController {
private appModule: App;
private tonConnectObserver: TonConnectObserver;
private documentObserver: DocumentObserver;
private webViewObserver: WebViewObserver;
private eventsThreshold: Record<string, number> = {
'app-hide': 3,
};
constructor(app: App) {
this.appModule = app;
this.documentObserver = new DocumentObserver(this);
this.tonConnectObserver = new TonConnectObserver(this);
this.webViewObserver = new WebViewObserver(this);
}
public async init() {
this.documentObserver.init();
this.tonConnectObserver.init();
this.webViewObserver.init();
try {
this.eventsThreshold = await (
await fetch(
(this.appModule.env === 'STG' ? STAGING_BACKEND_URL : BACKEND_URL) + 'events/threshold',
{
signal: AbortSignal.timeout(2000),
}
)
).json();
} catch (e) {
console.error(e);
}
}
public recordEvent(event_name: string, data?: Record<string, any>, userId?: string) {
this.appModule.recordEvent(event_name, data, undefined, userId).catch(e => console.error(e));
}
public async collectEvent(event_name: string, data?: Record<string, any>, userId?: string) {
if (this.eventsThreshold[event_name] === 0) {
return;
}
await this.appModule.collectEvent(event_name, data, userId);
if (this.eventsThreshold[event_name]) {
this.eventsThreshold[event_name]--;
}
}
}