-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient.ts
More file actions
100 lines (83 loc) · 2.77 KB
/
client.ts
File metadata and controls
100 lines (83 loc) · 2.77 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
import type { AptabaseOptions } from "./types";
import type { EnvironmentInfo } from "./env";
import { NativeEventDispatcher, WebEventDispatcher } from "./dispatcher";
import { newSessionId } from "./session";
import { HOSTS, SESSION_TIMEOUT } from "./constants";
export class AptabaseClient {
private readonly _dispatcher:
| WebEventDispatcher
| NativeEventDispatcher
| null;
private readonly _env: EnvironmentInfo;
private _sessionId = newSessionId();
private _lastTouched = new Date();
private _flushTimer: NodeJS.Timeout | undefined;
constructor(appKey: string, env: EnvironmentInfo, options?: AptabaseOptions) {
const [_, region] = appKey.split("-");
const baseUrl = this.getBaseUrl(region, options);
this._env = { ...env };
if (options?.appVersion) {
this._env.appVersion = options.appVersion;
}
const isWeb = this._env.osName === "web";
const isWebTrackingEnabled = isWeb && options?.enableWeb === true;
const shouldEnableTracking = !isWeb || isWebTrackingEnabled;
const dispatcher = shouldEnableTracking
? isWeb
? new WebEventDispatcher(appKey, baseUrl, env)
: new NativeEventDispatcher(appKey, baseUrl, env)
: null;
this._dispatcher = dispatcher;
}
public trackEvent(
eventName: string,
props?: Record<string, string | number | boolean>
) {
if (!this._dispatcher) return;
const isWeb = this._env.osName === "web";
this._dispatcher.enqueue({
timestamp: new Date().toISOString(),
sessionId: this.evalSessionId(),
eventName: eventName,
systemProps: {
isDebug: this._env.isDebug,
locale: this._env.locale,
osName: isWeb ? undefined : this._env.osName,
osVersion: isWeb ? undefined : this._env.osVersion,
appVersion: this._env.appVersion,
appBuildNumber: this._env.appBuildNumber,
sdkVersion: this._env.sdkVersion,
},
props: props,
});
}
public startPolling(flushInterval: number) {
this.stopPolling();
this._flushTimer = setInterval(this.flush.bind(this), flushInterval);
}
public stopPolling() {
if (this._flushTimer) {
clearInterval(this._flushTimer);
this._flushTimer = undefined;
}
}
public flush(): Promise<void> {
if (!this._dispatcher) return Promise.resolve();
return this._dispatcher.flush();
}
private evalSessionId() {
let now = new Date();
const diffInMs = now.getTime() - this._lastTouched.getTime();
if (diffInMs > SESSION_TIMEOUT) {
this._sessionId = newSessionId();
}
this._lastTouched = now;
return this._sessionId;
}
private getBaseUrl(region: string, options?: AptabaseOptions): string {
if (region === "SH") {
return options?.host ?? HOSTS.DEV;
}
return HOSTS[region];
}
}