Get User Agents Time Series

Get a time series of the percentage distribution of traffic of the top user agents.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get User Agents Time Series
8
 * Get a time series of the percentage distribution of traffic of the top user agents.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  aggInterval: "15m" | "1h" | "1d" | "1w" | undefined,
13
  name: string | undefined,
14
  dateRange: string | undefined,
15
  dateStart: string | undefined,
16
  dateEnd: string | undefined,
17
  asn: string | undefined,
18
  location: string | undefined,
19
  botClass: string | undefined,
20
  deviceType: string | undefined,
21
  httpProtocol: string | undefined,
22
  httpVersion: string | undefined,
23
  ipVersion: string | undefined,
24
  os: string | undefined,
25
  tlsVersion: string | undefined,
26
  limitPerGroup: string | undefined,
27
  format: "JSON" | "CSV" | undefined
28
) {
29
  const url = new URL(
30
    `https://api.cloudflare.com/client/v4/radar/http/timeseries_groups/browser`
31
  );
32
  for (const [k, v] of [
33
    ["aggInterval", aggInterval],
34
    ["name", name],
35
    ["dateRange", dateRange],
36
    ["dateStart", dateStart],
37
    ["dateEnd", dateEnd],
38
    ["asn", asn],
39
    ["location", location],
40
    ["botClass", botClass],
41
    ["deviceType", deviceType],
42
    ["httpProtocol", httpProtocol],
43
    ["httpVersion", httpVersion],
44
    ["ipVersion", ipVersion],
45
    ["os", os],
46
    ["tlsVersion", tlsVersion],
47
    ["limitPerGroup", limitPerGroup],
48
    ["format", format],
49
  ]) {
50
    if (v !== undefined && v !== "") {
51
      url.searchParams.append(k, v);
52
    }
53
  }
54
  const response = await fetch(url, {
55
    method: "GET",
56
    headers: {
57
      "X-AUTH-EMAIL": auth.email,
58
      "X-AUTH-KEY": auth.key,
59
      Authorization: "Bearer " + auth.token,
60
    },
61
    body: undefined,
62
  });
63
  if (!response.ok) {
64
    const text = await response.text();
65
    throw new Error(`${response.status} ${text}`);
66
  }
67
  return await response.json();
68
}
69