0
Table
One script reply has been approved by the moderators Verified

Retrieves a list of summarised aggregate metrics over a given time period.

See Analytics API properties for detailed information about the available query parameters.

Created by hugo697 103 days ago Viewed 5 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 103 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Table
8
 * Retrieves a list of summarised aggregate metrics over a given time period.
9

10
See [Analytics API properties](https://developers.cloudflare.com/dns/reference/analytics-api-properties/) for detailed information about the available query parameters.
11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  identifier: string,
15
  account_identifier: string,
16
  metrics: string | undefined,
17
  dimensions: string | undefined,
18
  since: string | undefined,
19
  until: string | undefined,
20
  limit: string | undefined,
21
  sort: string | undefined,
22
  filters: string | undefined
23
) {
24
  const url = new URL(
25
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/dns_firewall/${identifier}/dns_analytics/report`
26
  );
27
  for (const [k, v] of [
28
    ["metrics", metrics],
29
    ["dimensions", dimensions],
30
    ["since", since],
31
    ["until", until],
32
    ["limit", limit],
33
    ["sort", sort],
34
    ["filters", filters],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      "X-AUTH-EMAIL": auth.email,
44
      "X-AUTH-KEY": auth.key,
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: undefined,
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55