-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathindex.ts
More file actions
41 lines (34 loc) · 1.01 KB
/
index.ts
File metadata and controls
41 lines (34 loc) · 1.01 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
import { Client } from "./lib/client";
import type { ClientOpts } from "./lib/client";
import { trackPageview as _trackPageview } from "./lib/track";
import type { TrackPageviewOpts } from "./lib/track";
const GLOBALS = {
client: undefined as Client | undefined,
};
export function init(opts: ClientOpts) {
if (GLOBALS.client) {
return;
}
GLOBALS.client = new Client(opts);
}
export function isInitialized() {
return Boolean(GLOBALS.client);
}
export function getInitializedClient(): typeof GLOBALS["client"] {
return GLOBALS.client
}
export function trackPageview(opts?: TrackPageviewOpts) {
if (!GLOBALS.client) {
throw new Error(
"You must call Counterscale.initialize() before calling Counterscale.trackPageview().",
);
}
_trackPageview(GLOBALS.client, opts);
}
export function cleanup() {
if (!GLOBALS.client) {
return; // no-op if not already initialized (TODO: warn?)
}
GLOBALS.client.cleanup();
GLOBALS.client = undefined;
}