Get an academy's analytics

Script actimo Verified

by hugo697 ยท 11/5/2024

The script

Submitted by hugo697 Bun
Verified 572 days ago
1
//native
2
type Actimo = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	auth: Actimo,
8
	id: string,
9
	department: string | undefined,
10
	group: string | undefined,
11
	templateMessageId: string | undefined
12
) {
13
	const url = new URL(`https://actimo.com/api/v1/academy/${id}/analytics`)
14

15
	for (const [k, v] of [
16
		['department', department],
17
		['group', group],
18
		['templateMessageId', templateMessageId]
19
	]) {
20
		if (v !== undefined && v !== '' && k !== undefined) {
21
			url.searchParams.append(k, v)
22
		}
23
	}
24

25
	const response = await fetch(url, {
26
		method: 'GET',
27
		headers: {
28
			'api-key': auth.apiKey
29
		},
30
		body: undefined
31
	})
32

33
	if (!response.ok) {
34
		const text = await response.text()
35
		throw new Error(`${response.status} ${text}`)
36
	}
37

38
	return await response.json()
39
}
40