1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Metrics |
7 | * Get all metrics in an account. |
8 |
|
9 | Requests can be filtered by the following fields: |
10 | integration `name`, integration `category` |
11 |
|
12 | Returns a maximum of 200 results per page.*Rate limits*:Burst: `10/s`Steady: `150/m` |
13 |
|
14 | */ |
15 | export async function main( |
16 | auth: Klaviyo, |
17 | fields_flow_: string | undefined, |
18 | fields_metric_: string | undefined, |
19 | filter: string | undefined, |
20 | include: string | undefined, |
21 | page_cursor_: string | undefined, |
22 | revision: string, |
23 | ) { |
24 | const url = new URL(`https://a.klaviyo.com/api/metrics`); |
25 | for (const [k, v] of [ |
26 | ["fields[flow]", fields_flow_], |
27 | ["fields[metric]", fields_metric_], |
28 | ["filter", filter], |
29 | ["include", include], |
30 | ["page[cursor]", page_cursor_], |
31 | ]) { |
32 | if (v !== undefined && v !== "" && k !== undefined) { |
33 | url.searchParams.append(k, v); |
34 | } |
35 | } |
36 | const response = await fetch(url, { |
37 | method: "GET", |
38 | headers: { |
39 | revision: revision, |
40 | "Accept": "application/vnd.api+json", |
41 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
42 | }, |
43 | body: undefined, |
44 | }); |
45 | if (!response.ok) { |
46 | const text = await response.text(); |
47 | throw new Error(`${response.status} ${text}`); |
48 | } |
49 | return await response.json(); |
50 | } |
51 |
|