@tansy/analytics-fathom 0.0.0-stage → 0.1.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ What changed in each release of `@tansy/analytics-fathom`. Releases up to 0.1.0 went out with every other package
4
+ on one version and are described in [tansy's changelog](../../CHANGELOG.md).
5
+
6
+ ## 0.1.1 — 2026-11-22
7
+
8
+ No changes of its own. Released beside `tansy` 0.1.1, from which every package moves on its own
9
+ version, and it now requires it: the `tansy` peer range is ^0.1.1.
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-fathom
4
+
5
+ Fathom for @tansy/analytics: the tracker on every page, and its numbers on the dashboard.
6
+
7
+ ```bash
8
+ npm install @tansy/analytics-fathom
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,8 @@
1
+ export interface FathomOptions {
2
+ /** The site id, until one is saved in the settings. */
3
+ siteId?: string;
4
+ /** For tests: called instead of fetch. */
5
+ fetcher?: typeof fetch;
6
+ }
7
+ /** Fathom: no cookies, nothing personal. */
8
+ export declare const fathom: ({ siteId: defaultId, fetcher }?: FathomOptions) => import("@tansy/analytics/provider").AnalyticsProvider;
package/index.js ADDED
@@ -0,0 +1,65 @@
1
+ import { breakdown, defineAnalyticsProvider, fillDays, num, requestJson, str } from "@tansy/analytics/provider";
2
+ const CDN = "https://cdn.usefathom.com";
3
+ const siteId = (settings) => str(settings.fathomSiteId).toUpperCase();
4
+ const apiKey = (settings) => str(settings.fathomApiKey);
5
+ /** Fathom: no cookies, nothing personal. */
6
+ export const fathom = ({ siteId: defaultId = "", fetcher } = {}) => {
7
+ const aggregate = (settings, range, extra = {}) => requestJson(`https://api.usefathom.com/v1/aggregations?${new URLSearchParams({
8
+ entity: "pageview",
9
+ entity_id: siteId(settings),
10
+ aggregates: "pageviews,uniques",
11
+ date_from: `${range.from} 00:00:00`,
12
+ date_to: `${range.to} 23:59:59`,
13
+ timezone: "UTC",
14
+ ...extra,
15
+ })}`, { headers: { authorization: `Bearer ${apiKey(settings)}`, accept: "application/json" } }, fetcher);
16
+ const table = async (settings, range, field, by = "uniques") => breakdown((await aggregate(settings, range, { field_grouping: field, sort_by: `${by}:desc`, limit: "10" })).map((row) => ({ name: row[field], count: row[by] })));
17
+ return defineAnalyticsProvider({
18
+ id: "fathom",
19
+ label: "Fathom",
20
+ messages: {
21
+ ar: {
22
+ settings: {
23
+ fathomSiteId: { label: "معرّف الموقع في Fathom", description: "من Fathom ← الإعدادات ← المواقع. أحرف كبيرة، مثل ABCDEFGH." },
24
+ fathomApiKey: { label: "مفتاح API في Fathom", description: "اختياري، لعرض الأرقام هنا. من Fathom ← الإعدادات ← API، بصلاحية القراءة." },
25
+ },
26
+ },
27
+ },
28
+ settings: () => [
29
+ { name: "fathomSiteId", type: "text", default: defaultId, maxLength: 20, label: "Fathom site id", description: "From Fathom → Settings → Sites. Capital letters, such as ABCDEFGH." },
30
+ { name: "fathomApiKey", type: "secret", env: "FATHOM_API_KEY", label: "Fathom API key", description: "Optional, to show the numbers here. From Fathom → Settings → API, allowed to read." },
31
+ ],
32
+ ready: (settings) => /^[A-Z0-9]{4,20}$/.test(siteId(settings)),
33
+ reportReady: (settings) => Boolean(apiKey(settings)),
34
+ // Automatic tracking off, so a move between pages without a reload counts once, through the tracker.
35
+ tracker: (settings) => ({
36
+ script: `${CDN}/script.js`,
37
+ attributes: { "data-site": siteId(settings), "data-auto": "false" },
38
+ global: "fathom",
39
+ pageview: [{ fn: "fathom.trackPageview", args: [{ url: "$url", referrer: "$referrer" }] }],
40
+ }),
41
+ async report(range, settings) {
42
+ const [totals, daily, pages, referrers, countries, devices, browsers] = await Promise.all([
43
+ aggregate(settings, range),
44
+ aggregate(settings, range, { date_grouping: "day" }),
45
+ table(settings, range, "pathname", "pageviews"),
46
+ table(settings, range, "referrer_hostname"),
47
+ table(settings, range, "country_code"),
48
+ table(settings, range, "device_type"),
49
+ table(settings, range, "browser"),
50
+ ]);
51
+ const days = new Map(daily.map((row) => [String(row.date ?? "").slice(0, 10), { views: num(row.pageviews), visitors: num(row.uniques) }]));
52
+ return {
53
+ totals: { views: num(totals[0]?.pageviews), visitors: num(totals[0]?.uniques) },
54
+ days: fillDays(range, days),
55
+ pages,
56
+ referrers,
57
+ countries,
58
+ devices,
59
+ browsers,
60
+ };
61
+ },
62
+ dashboardUrl: () => "https://app.usefathom.com/",
63
+ csp: { connect: [CDN], img: [CDN] },
64
+ });
65
+ };
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@tansy/analytics-fathom",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Fathom 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-fathom"
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.1",
26
+ "tansy": "^0.1.1"
27
+ }
28
+ }