@tansy/analytics-umami 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mohamed Salah
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ <!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ # @tansy/analytics-umami
4
+
5
+ Umami for @tansy/analytics: the tracker on every page, and its numbers on the dashboard.
6
+
7
+ ```bash
8
+ npm install @tansy/analytics-umami
9
+ ```
10
+
11
+ Add it to `tansy.config.ts`. See [the analytics plugin documentation](https://github.com/mhmdsalahsebai/tansy/blob/main/docs/plugins/analytics.md) for what it adds and the options it takes.
12
+
13
+ A first-party plugin for [tansy](https://github.com/mhmdsalahsebai/tansy), a self-hosted, runtime-schema CMS for Next.js. MIT licensed.
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export interface UmamiOptions {
2
+ /** A self-hosted Umami's address. Umami Cloud when left out. */
3
+ host?: string;
4
+ /** The website's id, until one is saved in the settings. */
5
+ websiteId?: string;
6
+ /** For tests: called instead of fetch. */
7
+ fetcher?: typeof fetch;
8
+ }
9
+ /** Umami: no cookies, open source, in its cloud or self-hosted. */
10
+ export declare const umami: ({ host, websiteId: defaultId, fetcher }?: UmamiOptions) => import("@tansy/analytics/provider").AnalyticsProvider;
package/index.js ADDED
@@ -0,0 +1,79 @@
1
+ import { breakdown, defineAnalyticsProvider, fillDays, num, originOf, requestJson, str } from "@tansy/analytics/provider";
2
+ const CLOUD = "https://cloud.umami.is";
3
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
4
+ const websiteId = (settings) => str(settings.umamiWebsiteId);
5
+ const apiKey = (settings) => str(settings.umamiApiKey);
6
+ const valueOf = (value) => num(typeof value === "object" ? value?.value : value);
7
+ /** Umami: no cookies, open source, in its cloud or self-hosted. */
8
+ export const umami = ({ host = CLOUD, websiteId: defaultId = "", fetcher } = {}) => {
9
+ const origin = originOf(host, "Umami's host");
10
+ const cloud = origin === CLOUD;
11
+ // Umami Cloud's API is on its own host and takes an API key. A self-hosted one takes a token.
12
+ const api = cloud ? "https://api.umami.is/v1" : `${origin}/api`;
13
+ const get = (settings, path, range, extra = {}) => {
14
+ const query = new URLSearchParams({ startAt: String(Date.parse(`${range.from}T00:00:00Z`)), endAt: String(Date.parse(`${range.to}T23:59:59.999Z`)), ...extra });
15
+ return requestJson(`${api}/websites/${encodeURIComponent(websiteId(settings))}${path}?${query}`, {
16
+ headers: { accept: "application/json", ...(cloud ? { "x-umami-api-key": apiKey(settings) } : { authorization: `Bearer ${apiKey(settings)}` }) },
17
+ }, fetcher);
18
+ };
19
+ const metric = (settings, range, type) => get(settings, "/metrics", range, { type, limit: "10" }).then((rows) => breakdown(rows.map((row) => ({ name: row.x, count: row.y }))));
20
+ return defineAnalyticsProvider({
21
+ id: "umami",
22
+ label: "Umami",
23
+ messages: {
24
+ ar: {
25
+ settings: {
26
+ umamiWebsiteId: { label: "معرّف الموقع في Umami", description: "من Umami ← الإعدادات ← المواقع ← تعديل. يشبه 4fb7fa4c-5b46-438d-94b3-3a8fb9bc2e8b." },
27
+ umamiApiKey: { label: "مفتاح API في Umami", description: "اختياري، لعرض الأرقام هنا. مفتاح API من Umami Cloud، أو رمز دخول من Umami مستضاف ذاتيًا." },
28
+ },
29
+ },
30
+ },
31
+ settings: () => [
32
+ { name: "umamiWebsiteId", type: "text", default: defaultId, maxLength: 36, label: "Umami website id", description: "From Umami → Settings → Websites → Edit. It looks like 4fb7fa4c-5b46-438d-94b3-3a8fb9bc2e8b." },
33
+ { name: "umamiApiKey", type: "secret", env: "UMAMI_API_KEY", label: "Umami API key", description: "Optional, to show the numbers here. An API key from Umami Cloud, or a sign-in token from a self-hosted Umami." },
34
+ ],
35
+ ready: (settings) => UUID.test(websiteId(settings)),
36
+ reportReady: (settings) => Boolean(apiKey(settings)),
37
+ // Automatic tracking off, so a move between pages without a reload counts once, through the tracker.
38
+ tracker: (settings) => ({
39
+ script: `${origin}/script.js`,
40
+ attributes: { "data-website-id": websiteId(settings), "data-auto-track": "false" },
41
+ global: "umami",
42
+ pageview: [{ fn: "umami.track" }],
43
+ }),
44
+ async report(range, settings) {
45
+ const [stats, series, pages, referrers, countries, devices, browsers] = await Promise.all([
46
+ get(settings, "/stats", range),
47
+ get(settings, "/pageviews", range, { unit: "day", timezone: "UTC" }),
48
+ // Umami 2 calls an address "url", later versions "path".
49
+ metric(settings, range, "url").catch(() => metric(settings, range, "path")),
50
+ metric(settings, range, "referrer"),
51
+ metric(settings, range, "country"),
52
+ metric(settings, range, "device"),
53
+ metric(settings, range, "browser"),
54
+ ]);
55
+ const days = new Map();
56
+ const add = (rows, field) => {
57
+ for (const row of rows ?? []) {
58
+ const date = String(row.x ?? "").slice(0, 10);
59
+ const day = days.get(date) ?? { views: 0, visitors: 0 };
60
+ day[field] = num(row.y);
61
+ days.set(date, day);
62
+ }
63
+ };
64
+ add(series.pageviews, "views");
65
+ add(series.sessions, "visitors");
66
+ return {
67
+ totals: { views: valueOf(stats.pageviews), visitors: valueOf(stats.visitors) },
68
+ days: fillDays(range, days),
69
+ pages,
70
+ referrers,
71
+ countries,
72
+ devices,
73
+ browsers,
74
+ };
75
+ },
76
+ dashboardUrl: (settings) => `${origin}/websites/${encodeURIComponent(websiteId(settings))}`,
77
+ csp: { connect: cloud ? [origin, "https://api-gateway.umami.dev"] : [origin] },
78
+ });
79
+ };
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@tansy/analytics-umami",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Umami for @tansy/analytics: the tracker on every page, and its numbers on the dashboard.",
5
+ "keywords": [
6
+ "tansy",
7
+ "tansy-analytics-provider"
8
+ ],
9
+ "license": "MIT",
10
+ "author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
11
+ "homepage": "https://github.com/mhmdsalahsebai/tansy",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/mhmdsalahsebai/tansy.git",
15
+ "directory": "plugins/analytics-umami"
16
+ },
17
+ "bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./index.d.ts",
21
+ "default": "./index.js"
22
+ }
23
+ },
24
+ "peerDependencies": {
25
+ "@tansy/analytics": "^0.1.0",
26
+ "tansy": "^0.1.0"
27
+ }
28
+ }