-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
145 lines (120 loc) · 4.62 KB
/
index.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// deno-lint-ignore-file no-explicit-any
import type {
Aggregated,
Breakdowns,
Datapoints,
Interval,
Metrics,
Period,
Properties,
} from "./types.ts";
export default class Plausible {
private url = "https://plausible.com";
private key: string;
private site: string;
constructor(key: string, site: string, url?: string | null) {
this.key = key;
this.site = site;
if (url) {
// Remove the / if it has been accidentally provided
if (url.charAt(url.length) === "/") url = url.slice(0, -1);
this.url = url;
}
}
private async getAbstract(
path: string,
params: URLSearchParams = new URLSearchParams(),
): Promise<any> {
// Append the standard site ID to every request
params.append("site_id", this.site);
// Construct the endpoint URL and fetch the results
const endpoint = `${this.url}/${path}?${params}`;
const response = await fetch(endpoint, {
method: `GET`,
headers: {
Authorization: `Bearer ${this.key}`,
"Content-Type": `application/json`,
},
});
// Throw the error if the API has provided one
const parsed = (await response.json()) as any;
if (parsed.error) throw new Error(parsed.error);
// Otherwise just return the results
return parsed;
}
/**
* This function returns the number of current visitors on your site. A current visitor is defined as a visitor who triggered a pageview on your site in the last 5 minutes.
*/
public getRealtime(): Promise<number> {
return this.getAbstract(`api/v1/stats/realtime/visitors`);
}
/**
* This function aggregates metrics over a certain time period. If you are familiar with the Plausible dashboard, this function corresponds to the top row of stats that include Unique Visitors, Pageviews, Bounce rate and Visit duration. You can retrieve any number and combination of these metrics in one request.
*/
public async getAggregate<Compare extends boolean>(
period: Period,
metric: Metrics,
compare?: Compare | null,
filters?: string | null,
date?: string | null,
): Promise<Aggregated<Compare>> {
const params = new URLSearchParams();
params.append(`period`, period);
params.append(`metrics`, metric);
if (compare) params.append(`compare`, "previous_period");
if (filters) params.append("filters", filters);
if (date) params.append("date", date);
const response = await this.getAbstract(`api/v1/stats/aggregate`, params);
if (response.results.visitors) return response.results.visitors;
if (response.results.pageviews) return response.results.pageviews;
if (response.results.bounce_rate) return response.results.bounce_rate;
return response.results.visit_duration;
}
/**
* This function provides timeseries data over a certain time period. If you are familiar with the Plausible dashboard, this function corresponds to the main visitor graph.
*/
public async getTimeseries<Metric extends Metrics>(
period: Period,
metric: Metric,
filters?: string | null,
interval?: Interval | null,
date?: string | null,
): Promise<Datapoints<Metric>> {
const params = new URLSearchParams();
params.append(`period`, period);
if (metric) params.append(`metrics`, metric);
if (filters) params.append(`filters`, filters);
if (interval) params.append("interval", interval);
if (date) params.append("date", date);
const response = await this.getAbstract(`api/v1/stats/timeseries`, params);
return response.results;
}
/**
* This function allows you to breakdown your stats by some property. If you are familiar with SQL family databases, this function corresponds to running GROUP BY on a certain property in your stats.
*
* Check out the [properties](https://plausible.io/docs/stats-api#properties) section for a reference of all the properties you can use in this query.
*/
public async getBreakdown<
Property extends Properties,
Metric extends Metrics,
>(
period: Period,
metric: Metric,
property: Property,
filter?: string | null,
limit?: number | null,
page?: number | null,
date?: string | null,
): Promise<Breakdowns<Property, Metric>> {
const params = new URLSearchParams();
params.append(`period`, period);
params.append(`metrics`, metric);
params.append(`property`, property);
if (filter) params.append(`filters`, filter);
if (page) params.append("page", page.toString());
if (limit) params.append("limit", limit.toString());
if (date) params.append("date", date);
const response = await this.getAbstract(`api/v1/stats/breakdown`, params);
return response.results;
}
}