umami-api-js 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,3 +24,24 @@ yarn add umami-api-js # if using yarn
24
24
  pnpm add umami-api-js # if using pnpm
25
25
  bun a umami-api-js # if using bun
26
26
  ```
27
+
28
+ Finally, you will want to create an API object, which you will use for essentially all of your requests. You can do something like that:
29
+
30
+ ```typescript
31
+ // TypeScript
32
+ import * as umami from "umami-api-js"
33
+
34
+ // The API of self-hosted Umami instances authenticates with the credentials of actual accounts
35
+ const api = new umami.API("https://visitors.taevas.xyz/api", "<username>", "<password>") // first argument being the API route of a self-hosted Umami instance
36
+
37
+ // The website_id is featured in multiple places, like where you'd see the analytics script or the settings interface
38
+ async function displayStats(website_id: string) {
39
+ const now = new Date();
40
+ const sevendaysago = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
41
+ const stats = await api.getWebsiteStats(website_id, {startAt: sevendaysago, endAt: now, timezone: "UTC"});
42
+
43
+ console.log(`This website recently had ${stats.visits} visits from ${stats.visitors} visitors!`);
44
+ }
45
+
46
+ displayStats("f196d626-e609-4841-9a80-0dc60f523ed5");
47
+ ```
package/dist/index.d.ts CHANGED
@@ -93,7 +93,7 @@ export declare class APIError extends Error {
93
93
  * @param method The method used for this request (like "get", "post", etc...)
94
94
  * @param endpoint The type of resource that was requested from the server
95
95
  * @param parameters The filters that were used to specify what resource was wanted
96
- * @param status_code The status code that was returned by the server, if there is one
96
+ * @param response The status code and the original body that was returned by the server, if there is one
97
97
  * @param original_error The error that caused the api to throw an {@link APIError} in the first place, if there is one
98
98
  */
99
99
  constructor(message: string, server: API["server"], method: Parameters<API["request"]>[0], endpoint: Parameters<API["request"]>[1], parameters: Parameters<API["request"]>[2], response?: {
@@ -277,7 +277,7 @@ export declare class API {
277
277
  data?: {
278
278
  [k: string]: any;
279
279
  };
280
- }): Promise<{
280
+ }, type?: "event" | "identify"): Promise<{
281
281
  cache: string;
282
282
  sessionId: Events.Event["sessionId"];
283
283
  visitId: string;
package/dist/index.js CHANGED
@@ -27,7 +27,7 @@ export class APIError extends Error {
27
27
  * @param method The method used for this request (like "get", "post", etc...)
28
28
  * @param endpoint The type of resource that was requested from the server
29
29
  * @param parameters The filters that were used to specify what resource was wanted
30
- * @param status_code The status code that was returned by the server, if there is one
30
+ * @param response The status code and the original body that was returned by the server, if there is one
31
31
  * @param original_error The error that caused the api to throw an {@link APIError} in the first place, if there is one
32
32
  */
33
33
  constructor(message, server, method, endpoint, parameters, response, original_error) {
@@ -423,8 +423,8 @@ export class API {
423
423
  * To register an event
424
424
  * @group Sending stats
425
425
  */
426
- async sendStats(websiteId, payload) {
427
- return await this.request("post", ["send"], { payload: { website: websiteId, ...payload }, type: "event" });
426
+ async sendStats(websiteId, payload, type = "event") {
427
+ return await this.request("post", ["send"], { payload: { website: websiteId, ...payload }, type });
428
428
  }
429
429
  // ADMIN
430
430
  /** @group Admin endpoints */
@@ -46,9 +46,14 @@ export declare namespace Events {
46
46
  eventName: Event["eventName"];
47
47
  propertyName: string;
48
48
  dataType: number;
49
+ propertyValue: string | number;
49
50
  total: number;
50
51
  }
51
- /** Gets event data names, properties, and counts: https://umami.is/docs/api/events#get-apiwebsiteswebsiteidevent-dataevents (TODO Server returns a 500) */
52
+ /**
53
+ * Gets event data names, properties, and counts: https://umami.is/docs/api/events#get-apiwebsiteswebsiteidevent-dataevents
54
+ * @remarks At least on Umami v3.0.3 and likely prior versions, **this will not work if not using the optional event name filter**
55
+ * https://github.com/umami-software/umami/issues/3837
56
+ */
52
57
  function get_WEBSITEID_EventdataEvents(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & {
53
58
  /** Event name filter */
54
59
  event?: Event["eventName"];
@@ -12,7 +12,11 @@ export var Events;
12
12
  return await this.request("get", ["websites", websiteId, "event-data", eventId]);
13
13
  }
14
14
  Events.get_WEBSITEID_Eventdata_EVENTID = get_WEBSITEID_Eventdata_EVENTID;
15
- /** Gets event data names, properties, and counts: https://umami.is/docs/api/events#get-apiwebsiteswebsiteidevent-dataevents (TODO Server returns a 500) */
15
+ /**
16
+ * Gets event data names, properties, and counts: https://umami.is/docs/api/events#get-apiwebsiteswebsiteidevent-dataevents
17
+ * @remarks At least on Umami v3.0.3 and likely prior versions, **this will not work if not using the optional event name filter**
18
+ * https://github.com/umami-software/umami/issues/3837
19
+ */
16
20
  async function get_WEBSITEID_EventdataEvents(websiteId, parameters) {
17
21
  return await this.request("get", ["websites", websiteId, "event-data", "events"], parameters);
18
22
  }
@@ -7,7 +7,7 @@ export declare namespace Sessions {
7
7
  browser: string | null;
8
8
  os: string | null;
9
9
  device: string;
10
- screen: string;
10
+ screen: string | null;
11
11
  language: string | null;
12
12
  country: string;
13
13
  region: string;
@@ -98,13 +98,13 @@ export declare namespace Sessions {
98
98
  dateValue: Date | null;
99
99
  createdAt: Date;
100
100
  }
101
- /** Gets session properties for a individual session: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsessionssessionidproperties (TODO Server returns empty array) */
101
+ /** Gets session properties for a individual session: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsessionssessionidproperties */
102
102
  function get_WEBSITEID_Sessions_SESSIONID_Properties(this: API, websiteId: Websites.Website["id"], sessionId: Session["id"]): Promise<Properties[]>;
103
103
  interface DataProperties {
104
104
  propertyName: string;
105
105
  total: number;
106
106
  }
107
- /** Gets session data counts by property name: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-dataproperties (TODO Server returns empty array) */
107
+ /** Gets session data counts by property name: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-dataproperties */
108
108
  function get_WEBSITEID_SessiondataProperties(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & {
109
109
  /** Can accept filter parameters */
110
110
  filters?: Filters;
@@ -113,7 +113,7 @@ export declare namespace Sessions {
113
113
  value: string;
114
114
  total: number;
115
115
  }
116
- /** Gets session data counts for a given property: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-datavalues (TODO Server returns empty array) */
116
+ /** Gets session data counts for a given property: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-datavalues */
117
117
  function get_WEBSITEID_SessiondataValues(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & {
118
118
  /** Property name */
119
119
  propertyName: string;
@@ -29,17 +29,17 @@ export var Sessions;
29
29
  return await this.request("get", ["websites", websiteId, "sessions", sessionId, "activity"], parameters);
30
30
  }
31
31
  Sessions.get_WEBSITEID_Sessions_SESSIONID_Activity = get_WEBSITEID_Sessions_SESSIONID_Activity;
32
- /** Gets session properties for a individual session: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsessionssessionidproperties (TODO Server returns empty array) */
32
+ /** Gets session properties for a individual session: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsessionssessionidproperties */
33
33
  async function get_WEBSITEID_Sessions_SESSIONID_Properties(websiteId, sessionId) {
34
34
  return await this.request("get", ["websites", websiteId, "sessions", sessionId, "properties"]);
35
35
  }
36
36
  Sessions.get_WEBSITEID_Sessions_SESSIONID_Properties = get_WEBSITEID_Sessions_SESSIONID_Properties;
37
- /** Gets session data counts by property name: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-dataproperties (TODO Server returns empty array) */
37
+ /** Gets session data counts by property name: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-dataproperties */
38
38
  async function get_WEBSITEID_SessiondataProperties(websiteId, parameters) {
39
39
  return await this.request("get", ["websites", websiteId, "session-data", "properties"], parameters);
40
40
  }
41
41
  Sessions.get_WEBSITEID_SessiondataProperties = get_WEBSITEID_SessiondataProperties;
42
- /** Gets session data counts for a given property: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-datavalues (TODO Server returns empty array) */
42
+ /** Gets session data counts for a given property: https://umami.is/docs/api/sessions#get-apiwebsiteswebsiteidsession-datavalues */
43
43
  async function get_WEBSITEID_SessiondataValues(websiteId, parameters) {
44
44
  return await this.request("get", ["websites", websiteId, "session-data", "values"], parameters);
45
45
  }
@@ -30,10 +30,12 @@ export declare namespace WebsiteStats {
30
30
  totaltime: number;
31
31
  }
32
32
  type MetricsTypes = "path" | "entry" | "exit" | "title" | "query" | "referrer" | "channel" | "domain" | "country" | "region" | "city" | "browser" | "os" | "device" | "language" | "screen" | "event" | "hostname" | "tag";
33
- /** Gets the number of active users on a website: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidactive */
34
- function get_WEBSITEID_Active(this: API, websiteId: Websites.Website["id"]): Promise<{
33
+ interface Active {
34
+ /** Number of unique visitors within the last 5 minutes */
35
35
  visitors: number;
36
- }>;
36
+ }
37
+ /** Gets the number of active users on a website: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidactive */
38
+ function get_WEBSITEID_Active(this: API, websiteId: Websites.Website["id"]): Promise<Active>;
37
39
  /** Gets events within a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteideventsseries */
38
40
  function get_WEBSITEID_EventsSeries(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & Units & {
39
41
  /** Can accept filter parameters */
@@ -41,7 +43,13 @@ export declare namespace WebsiteStats {
41
43
  /** Timezone (ex. America/Los_Angeles) */
42
44
  timezone: string;
43
45
  }): Promise<XTY[]>;
44
- /** Gets metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetrics (TODO UNTESTED) */
46
+ interface Metrics {
47
+ /** Unique value, depending on metric type */
48
+ x: string;
49
+ /** Number of visitors */
50
+ y: number;
51
+ }
52
+ /** Gets metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetrics */
45
53
  function get_WEBSITEID_Metrics(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & Units & {
46
54
  /** Timezone (ex. America/Los_Angeles) */
47
55
  timezone: string;
@@ -53,26 +61,14 @@ export declare namespace WebsiteStats {
53
61
  limit?: number;
54
62
  /** (optional, default 0) Number of ows to skip */
55
63
  offset?: number;
56
- }): Promise<{
57
- /** Unique value, depending on metric type */
58
- x: string;
59
- /** Number of visitors */
60
- y: number;
61
- }[]>;
62
- /** Gets expanded metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetricsexpanded (TODO UNTESTED) */
63
- function get_WEBSITEID_MetricsExpanded(this: API, ...args: Parameters<typeof get_WEBSITEID_Metrics>): Promise<(Stats & {
64
+ }): Promise<Metrics[]>;
65
+ interface ExpandedMetrics extends Stats {
64
66
  /** Unique value, depending on metric type */
65
67
  name: string;
66
- })[]>;
67
- /** Gets pageviews within a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidpageviews (TODO UNTESTED) */
68
- function get_WEBSITEID_Pageviews(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & Units & {
69
- /** Timezone (ex. America/Los_Angeles) */
70
- timezone: string;
71
- /** Comparison value `prev` | `yoy` */
72
- compare?: string;
73
- /** Can accept filter parameters */
74
- filters?: Filters;
75
- }): Promise<{
68
+ }
69
+ /** Gets expanded metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetricsexpanded */
70
+ function get_WEBSITEID_MetricsExpanded(this: API, ...args: Parameters<typeof get_WEBSITEID_Metrics>): Promise<ExpandedMetrics[]>;
71
+ interface Pageviews {
76
72
  pageviews: {
77
73
  /** Timestamp */
78
74
  x: Date;
@@ -85,12 +81,22 @@ export declare namespace WebsiteStats {
85
81
  /** Number of pageviews or visitors */
86
82
  y: number;
87
83
  }[];
88
- }>;
89
- /** Gets summarized website statistics: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidstats (TODO UNTESTED) */
90
- function get_WEBSITEID_Stats(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & {
84
+ }
85
+ /** Gets pageviews within a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidpageviews */
86
+ function get_WEBSITEID_Pageviews(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & Units & {
87
+ /** Timezone (ex. America/Los_Angeles) */
88
+ timezone: string;
89
+ /** Comparison value `prev` | `yoy` */
90
+ compare?: string;
91
91
  /** Can accept filter parameters */
92
92
  filters?: Filters;
93
- }): Promise<Stats & {
93
+ }): Promise<Pageviews>;
94
+ interface StatsWithComparison extends Stats {
94
95
  comparison: Stats;
95
- }>;
96
+ }
97
+ /** Gets summarized website statistics: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidstats */
98
+ function get_WEBSITEID_Stats(this: API, websiteId: Websites.Website["id"], parameters: Timestamps & {
99
+ /** Can accept filter parameters */
100
+ filters?: Filters;
101
+ }): Promise<StatsWithComparison>;
96
102
  }
@@ -11,22 +11,22 @@ export var WebsiteStats;
11
11
  return await this.request("get", ["websites", websiteId, "events", "series"], parameters);
12
12
  }
13
13
  WebsiteStats.get_WEBSITEID_EventsSeries = get_WEBSITEID_EventsSeries;
14
- /** Gets metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetrics (TODO UNTESTED) */
14
+ /** Gets metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetrics */
15
15
  async function get_WEBSITEID_Metrics(websiteId, parameters) {
16
16
  return await this.request("get", ["websites", websiteId, "metrics"], parameters);
17
17
  }
18
18
  WebsiteStats.get_WEBSITEID_Metrics = get_WEBSITEID_Metrics;
19
- /** Gets expanded metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetricsexpanded (TODO UNTESTED) */
19
+ /** Gets expanded metrics for a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidmetricsexpanded */
20
20
  async function get_WEBSITEID_MetricsExpanded(...args) {
21
21
  return await this.request("get", ["websites", args[0], "metrics", "expanded"], args[1]);
22
22
  }
23
23
  WebsiteStats.get_WEBSITEID_MetricsExpanded = get_WEBSITEID_MetricsExpanded;
24
- /** Gets pageviews within a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidpageviews (TODO UNTESTED) */
24
+ /** Gets pageviews within a given time range: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidpageviews */
25
25
  async function get_WEBSITEID_Pageviews(websiteId, parameters) {
26
26
  return await this.request("get", ["websites", websiteId, "pageviews"], parameters);
27
27
  }
28
28
  WebsiteStats.get_WEBSITEID_Pageviews = get_WEBSITEID_Pageviews;
29
- /** Gets summarized website statistics: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidstats (TODO UNTESTED) */
29
+ /** Gets summarized website statistics: https://umami.is/docs/api/website-stats#get-apiwebsiteswebsiteidstats */
30
30
  async function get_WEBSITEID_Stats(websiteId, parameters) {
31
31
  return await this.request("get", ["websites", websiteId, "stats"], parameters);
32
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "umami-api-js",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Package to easily access the Umami api!",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -9,7 +9,7 @@
9
9
  "prepublish": "npm run build",
10
10
  "build": "tsc",
11
11
  "test": "npm run build && node ./dist/test.js",
12
- "docs": "npx typedoc lib/index.ts"
12
+ "docs": "npx typedoc lib/index.ts --cname umami-api-js.taevas.xyz --plugin ./docs_plugins/visitors.ts"
13
13
  },
14
14
  "engines": {
15
15
  "node": ">=20.0.0"