@tansy/analytics-plausible 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 +21 -0
- package/README.md +12 -2
- package/index.d.ts +10 -0
- package/index.js +63 -0
- package/package.json +26 -4
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
|
-
|
|
1
|
+
<!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @tansy/analytics-plausible
|
|
4
|
+
|
|
5
|
+
Plausible for @tansy/analytics: the tracker on every page, and its numbers on the dashboard.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tansy/analytics-plausible
|
|
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 PlausibleOptions {
|
|
2
|
+
/** A self-hosted Plausible's address. plausible.io when left out. */
|
|
3
|
+
host?: string;
|
|
4
|
+
/** The site's domain as Plausible knows it, until one is saved in the settings. */
|
|
5
|
+
domain?: string;
|
|
6
|
+
/** For tests: called instead of fetch. */
|
|
7
|
+
fetcher?: typeof fetch;
|
|
8
|
+
}
|
|
9
|
+
/** Plausible: no cookies, nothing personal, and EU-hosted. */
|
|
10
|
+
export declare const plausible: ({ host, domain: defaultDomain, fetcher }?: PlausibleOptions) => import("@tansy/analytics/provider").AnalyticsProvider;
|
package/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { breakdown, defineAnalyticsProvider, fillDays, num, originOf, requestJson, str } from "@tansy/analytics/provider";
|
|
2
|
+
const domain = (settings) => str(settings.plausibleDomain);
|
|
3
|
+
const apiKey = (settings) => str(settings.plausibleApiKey);
|
|
4
|
+
/** Plausible: no cookies, nothing personal, and EU-hosted. */
|
|
5
|
+
export const plausible = ({ host = "https://plausible.io", domain: defaultDomain = "", fetcher } = {}) => {
|
|
6
|
+
const origin = originOf(host, "Plausible's host");
|
|
7
|
+
/** One query to the Stats API v2. */
|
|
8
|
+
const query = (settings, range, metrics, dimensions = [], limit) => requestJson(`${origin}/api/v2/query`, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: { authorization: `Bearer ${apiKey(settings)}`, "content-type": "application/json" },
|
|
11
|
+
body: JSON.stringify({ site_id: domain(settings), metrics, dimensions, date_range: [range.from, range.to], ...(limit ? { order_by: [[metrics[0], "desc"]], pagination: { limit } } : {}) }),
|
|
12
|
+
}, fetcher).then((answer) => answer.results ?? []);
|
|
13
|
+
const rows = (results) => results.map((row) => ({ name: row.dimensions?.[0], count: row.metrics?.[0] }));
|
|
14
|
+
return defineAnalyticsProvider({
|
|
15
|
+
id: "plausible",
|
|
16
|
+
label: "Plausible",
|
|
17
|
+
messages: {
|
|
18
|
+
ar: {
|
|
19
|
+
settings: {
|
|
20
|
+
plausibleDomain: { label: "النطاق في Plausible", description: "نطاق الموقع كما أضفته في Plausible، مثل example.com." },
|
|
21
|
+
plausibleApiKey: { label: "مفتاح Stats API في Plausible", description: "اختياري، لعرض الأرقام هنا. من Plausible ← الإعدادات ← API Keys." },
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
settings: () => [
|
|
26
|
+
{ name: "plausibleDomain", type: "text", default: defaultDomain, maxLength: 200, label: "Plausible domain", description: "The site's domain as you added it in Plausible, such as example.com." },
|
|
27
|
+
{ name: "plausibleApiKey", type: "secret", env: "PLAUSIBLE_API_KEY", label: "Plausible Stats API key", description: "Optional, to show the numbers here. From Plausible → Settings → API Keys." },
|
|
28
|
+
],
|
|
29
|
+
ready: (settings) => /^[a-z0-9.-]+(\/[\w.-]+)?$/i.test(domain(settings)),
|
|
30
|
+
reportReady: (settings) => Boolean(apiKey(settings)),
|
|
31
|
+
// The manual script, so the tracker reports moves between pages that don't reload.
|
|
32
|
+
tracker: (settings) => ({
|
|
33
|
+
script: `${origin}/js/script.manual.js`,
|
|
34
|
+
attributes: { "data-domain": domain(settings) },
|
|
35
|
+
global: "plausible",
|
|
36
|
+
stubs: [{ type: "queue", name: "plausible", into: "plausible.q" }],
|
|
37
|
+
pageview: [{ fn: "plausible", args: ["pageview", { u: "$url" }] }],
|
|
38
|
+
}),
|
|
39
|
+
async report(range, settings) {
|
|
40
|
+
const [totals, daily, pages, sources, countries, devices, browsers] = await Promise.all([
|
|
41
|
+
query(settings, range, ["pageviews", "visitors"]),
|
|
42
|
+
query(settings, range, ["pageviews", "visitors"], ["time:day"]),
|
|
43
|
+
query(settings, range, ["pageviews"], ["event:page"], 10),
|
|
44
|
+
query(settings, range, ["visitors"], ["visit:source"], 10),
|
|
45
|
+
query(settings, range, ["visitors"], ["visit:country"], 10),
|
|
46
|
+
query(settings, range, ["visitors"], ["visit:device"], 10),
|
|
47
|
+
query(settings, range, ["visitors"], ["visit:browser"], 10),
|
|
48
|
+
]);
|
|
49
|
+
const days = new Map(daily.map((row) => [String(row.dimensions?.[0] ?? "").slice(0, 10), { views: num(row.metrics?.[0]), visitors: num(row.metrics?.[1]) }]));
|
|
50
|
+
return {
|
|
51
|
+
totals: { views: num(totals[0]?.metrics?.[0]), visitors: num(totals[0]?.metrics?.[1]) },
|
|
52
|
+
days: fillDays(range, days),
|
|
53
|
+
pages: breakdown(rows(pages)),
|
|
54
|
+
referrers: breakdown(rows(sources).filter((row) => row.name !== "Direct / None")),
|
|
55
|
+
countries: breakdown(rows(countries)),
|
|
56
|
+
devices: breakdown(rows(devices)),
|
|
57
|
+
browsers: breakdown(rows(browsers)),
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
dashboardUrl: (settings) => `${origin}/${encodeURIComponent(domain(settings))}`,
|
|
61
|
+
csp: { connect: [origin] },
|
|
62
|
+
});
|
|
63
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/analytics-plausible",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Plausible 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-plausible"
|
|
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
|
+
}
|